|
3 | 3 | import argparse |
4 | 4 | from collections.abc import Sequence |
5 | 5 |
|
| 6 | +from . import filter as filter_mvd |
| 7 | +from . import serialize |
6 | 8 | from .model import concept_root, template |
7 | 9 | from .parser import parse |
8 | 10 |
|
9 | 11 |
|
10 | 12 | def build_parser() -> argparse.ArgumentParser: |
11 | 13 | parser = argparse.ArgumentParser( |
12 | 14 | prog="python -m ifcopenshell.mvd", |
13 | | - description="Inspect mvdXML or execute its generated SPARQL against an IFC-OWL file.", |
| 15 | + description="Inspect, validate, extract, combine, or convert mvdXML documents.", |
14 | 16 | ) |
15 | | - parser.add_argument("mvdxml", help="Path to an mvdXML document") |
16 | | - parser.add_argument( |
| 17 | + subparsers = parser.add_subparsers(dest="command", required=True) |
| 18 | + |
| 19 | + inspect_parser = subparsers.add_parser("inspect", help="Inspect an mvdXML document") |
| 20 | + inspect_parser.add_argument("mvdxml", help="Path to an mvdXML document") |
| 21 | + |
| 22 | + validate_parser = subparsers.add_parser( |
| 23 | + "validate", |
| 24 | + help="Validate an IFC file against an mvdXML document", |
| 25 | + ) |
| 26 | + validate_parser.add_argument("mvdxml", help="Path to an mvdXML document") |
| 27 | + validate_parser.add_argument("ifc", help="Path to an IFC file") |
| 28 | + |
| 29 | + convert_parser = subparsers.add_parser( |
| 30 | + "convert", |
| 31 | + help="Generate and execute SPARQL against an IFC-OWL Turtle file", |
| 32 | + ) |
| 33 | + convert_parser.add_argument("mvdxml", help="Path to an mvdXML document") |
| 34 | + convert_parser.add_argument( |
17 | 35 | "ifcowl", |
18 | | - nargs="?", |
19 | | - help="Optional path to an IFC-OWL Turtle file; omit it to inspect the mvdXML", |
| 36 | + help="Path to an IFC-OWL Turtle file", |
20 | 37 | ) |
| 38 | + |
| 39 | + extract_parser = subparsers.add_parser( |
| 40 | + "extract", |
| 41 | + help="Extract a ConceptTemplate, ConceptRoot, or Concept into a new mvdXML document", |
| 42 | + ) |
| 43 | + extract_parser.add_argument("mvdxml", help="Path to the source mvdXML document") |
| 44 | + extract_parser.add_argument( |
| 45 | + "selection", |
| 46 | + help="ConceptTemplate, ConceptRoot, or ConceptRoot/Concept name", |
| 47 | + ) |
| 48 | + extract_parser.add_argument("output", help="Path to the output mvdXML document") |
| 49 | + |
| 50 | + combine_parser = subparsers.add_parser( |
| 51 | + "combine", |
| 52 | + help="Combine mvdXML documents", |
| 53 | + ) |
| 54 | + combine_parser.add_argument( |
| 55 | + "mvdxml", |
| 56 | + nargs="+", |
| 57 | + help="Paths to the source mvdXML documents", |
| 58 | + ) |
| 59 | + combine_parser.add_argument("output", help="Path to the output mvdXML document") |
21 | 60 | return parser |
22 | 61 |
|
23 | 62 |
|
@@ -52,16 +91,78 @@ def execute_sparql(parsed_root: concept_root, mvdxml: str, ifcowl: str) -> None: |
52 | 91 | print(sparql.executor.run(parsed_root, mvdxml, inferred_ifcowl), end="") |
53 | 92 |
|
54 | 93 |
|
| 94 | +def _parse_roots(mvdxml: str) -> list[concept_root]: |
| 95 | + roots = [item for item in parse(mvdxml) if isinstance(item, concept_root)] |
| 96 | + if not roots: |
| 97 | + raise ValueError(f"mvdXML document {mvdxml!r} contains no ConceptRoot") |
| 98 | + return roots |
| 99 | + |
| 100 | + |
| 101 | +def validate_mvd(mvdxml: str, ifc: str) -> bool: |
| 102 | + import ifcopenshell |
| 103 | + |
| 104 | + ifc_file = ifcopenshell.open(ifc) |
| 105 | + all_valid = True |
| 106 | + |
| 107 | + for root in _parse_roots(mvdxml): |
| 108 | + entities = list(ifc_file.by_type(root.entity)) |
| 109 | + applicability = root.parsed_applicability |
| 110 | + if applicability is not None: |
| 111 | + entities = [ |
| 112 | + entity |
| 113 | + for entity in entities |
| 114 | + if applicability.validate(applicability.template().extract(entity))[0] |
| 115 | + ] |
| 116 | + |
| 117 | + verification = {entity.GlobalId: {} for entity in entities} |
| 118 | + for entity in entities: |
| 119 | + for concept in root.concepts(): |
| 120 | + valid, _ = concept.validate(concept.template().extract(entity)) |
| 121 | + verification[entity.GlobalId][concept.name] = int(not valid) |
| 122 | + |
| 123 | + non_respecting = root.get_non_respecting_entities(ifc_file, verification) |
| 124 | + all_valid = all_valid and not non_respecting |
| 125 | + print( |
| 126 | + f"{root.name}: {len(non_respecting)} of " |
| 127 | + f"{len(entities)} applicable entities failed" |
| 128 | + ) |
| 129 | + for entity in non_respecting: |
| 130 | + failed_concepts = [ |
| 131 | + name for name, failed in verification[entity.GlobalId].items() if failed |
| 132 | + ] |
| 133 | + print(f" {entity.GlobalId}: {', '.join(failed_concepts)}") |
| 134 | + |
| 135 | + return all_valid |
| 136 | + |
| 137 | + |
55 | 138 | def main(argv: Sequence[str] | None = None) -> int: |
56 | 139 | args = build_parser().parse_args(argv) |
57 | | - if args.ifcowl is None: |
| 140 | + if args.command == "inspect": |
58 | 141 | inspect_mvd(args.mvdxml) |
59 | 142 | return 0 |
| 143 | + if args.command == "validate": |
| 144 | + return int(not validate_mvd(args.mvdxml, args.ifc)) |
| 145 | + if args.command == "extract": |
| 146 | + serialize( |
| 147 | + filter_mvd( |
| 148 | + parse(args.mvdxml, include_templates=True), |
| 149 | + args.selection, |
| 150 | + ), |
| 151 | + args.output, |
| 152 | + ) |
| 153 | + return 0 |
| 154 | + if args.command == "combine": |
| 155 | + serialize( |
| 156 | + ( |
| 157 | + item |
| 158 | + for filename in args.mvdxml |
| 159 | + for item in parse(filename, include_templates=True) |
| 160 | + ), |
| 161 | + args.output, |
| 162 | + ) |
| 163 | + return 0 |
60 | 164 |
|
61 | | - roots = [item for item in parse(args.mvdxml) if isinstance(item, concept_root)] |
62 | | - if not roots: |
63 | | - raise ValueError(f"mvdXML document {args.mvdxml!r} contains no ConceptRoot") |
64 | | - for root in roots: |
| 165 | + for root in _parse_roots(args.mvdxml): |
65 | 166 | execute_sparql(root, args.mvdxml, args.ifcowl) |
66 | 167 | return 0 |
67 | 168 |
|
|
0 commit comments