### Command Line: Display mvdXML Structure and Run SPARQL Validation Source: https://context7.com/opensourcebim/python-mvdxml/llms.txt Demonstrates command-line execution of python-mvdxml for displaying the structure of mvdXML files (concepts and rules) and for performing SPARQL validation on a TTL file against an mvdXML model. The output includes element counts, generated SPARQL queries, and validation results. ```bash # Display mvdXML structure (concepts and rules) python -m ifcopenshell.mvd model.mvdxml # Execute SPARQL validation on TTL file python -m ifcopenshell.mvd model.mvdxml model.ttl ``` -------------------------------- ### Create and Write New IFC File with Respecting Entities Source: https://github.com/opensourcebim/python-mvdxml/blob/master/README.md This code snippet shows how to create a new IFC file, copy the IfcProject definition from an existing file, and add only the entities that were identified as respecting the mvdXML concepts. The new file is then saved. ```python # Create a new file new_file = ifcopenshell.file(schema=file.schema) proj = file.by_type("IfcProject")[0] new_file.add(proj) for e in respecting_entities: new_file.add(e) new_file.write("new_file.ifc") ``` -------------------------------- ### Convert mvdXML to SPARQL Queries using Python Source: https://context7.com/opensourcebim/python-mvdxml/llms.txt This Python snippet demonstrates converting mvdXML concepts into SPARQL queries. It parses an mvdXML file, optionally infers subtype relationships from a TTL file, and then uses `ifcopenshell.mvd.sparql.executor.run` to execute SPARQL queries against linked IFC data. It also shows how to manually build a SPARQL query from a concept's applicability. ```python from ifcopenshell.mvd import concept_root, sparql # Parse mvdXML file mvd_file = "model.mvdxml" ttl_file = "model.ttl" # Derive ifcOwl prefix from TTL file sparql.derive_prefix(ttl_file) # Optional: infer subtype relationships ttl_file = sparql.infer_subtypes(ttl_file) # Parse concept roots and execute SPARQL queries for mvd in concept_root.parse(mvd_file): # Execute all SPARQL queries for this concept root # Generates .sparql files and executes them using Apache Jena # Outputs tables showing passing/failing elements sparql.executor.run(mvd, mvd_file, ttl_file) # Manual SPARQL generation example concept_roots = list(concept_root.parse(mvd_file)) cr = concept_roots[0] applicability = cr.applicability() # Convert concept to SPARQL query query_builder = sparql.convertor.concept_or_applicability(applicability) print(query_builder) # Output includes: # - PREFIX declarations # - SELECT clause with variable bindings # - WHERE clause with graph patterns # - FILTER clause for constraints ``` -------------------------------- ### Extract mvdXML Data and Entities from IFC File Source: https://github.com/opensourcebim/python-mvdxml/blob/master/README.md This snippet demonstrates how to open an mvdXML concept and an IFC file, then extract all data into a spreadsheet-like format. It also shows how to identify entities that respect or do not respect the mvdXML concepts. ```python import ifcopenshell from ifcopenshell.mvd import mvd mvd_concept = mvd.open_mvd("examples/wall_extraction.mvdxml") file = ifcopenshell.open("Duplex_A_20110505.ifc") all_data = mvd.get_data(mvd_concept, file, spreadsheet_export=True) non_respecting_entities = mvd.get_non_respecting_entities(file, all_data[1]) respecting_entities = mvd.get_respecting_entities(file, all_data[1]) ``` -------------------------------- ### Extract Data from Rules using Python Source: https://context7.com/opensourcebim/python-mvdxml/llms.txt This Python snippet demonstrates how to parse an mvdXML file, open an IFC file, and extract data from IFC instances based on the rules defined in a concept template. It utilizes `ifcopenshell.mvd.extract_data` to retrieve attribute values corresponding to the mvdXML rules, returning a list of dictionaries. ```python import ifcopenshell from ifcopenshell.mvd import mvd # Parse mvdXML to get concept with rules concept_roots = list(ifcopenshell.mvd.concept_root.parse("model.mvdxml")) concept_root = concept_roots[0] concept = list(concept_root.concepts())[0] # Get the rule tree from concept template rules_root = concept.template().rules[0] # Open IFC file and get instance ifc_file = ifcopenshell.open("model.ifc") instance = ifc_file.by_type(concept_root.entity)[0] # Extract data - returns list of dictionaries mapping rules to values data = mvd.extract_data(rules_root, instance) # Example output structure: # [ # {: 'abc123xyz', # : 'Wall-001', # : IfcBoolean('T')} # ] for result_dict in data: for rule, value in result_dict.items(): print(f"{rule.attribute}: {value}") ``` -------------------------------- ### Open mvdXML File with Python Source: https://context7.com/opensourcebim/python-mvdxml/llms.txt Parses and loads an mvdXML file to extract concept definitions. The function returns a concept_root object containing entity and name attributes. This is the initial step for any mvdXML-based operation. ```python import ifcopenshell from ifcopenshell.mvd import mvd # Open mvdXML file mvd_concept = mvd.open_mvd("mvd_examples/wall_extraction.mvdxml") # The returned object is a concept_root with entity and name attributes print(mvd_concept.entity) # e.g., "IfcWall" print(mvd_concept.name) # Name of the concept root ``` -------------------------------- ### Track validation statistics Source: https://context7.com/opensourcebim/python-mvdxml/llms.txt This snippet demonstrates how to iterate through ConceptRoots and Concepts from an mvdXML file, validating IFC instances against defined rules and tracking statistics for successful and failed tests. It includes error handling for exceptions during rule processing and data validation. ```python total_tests = 0 successful_tests = 0 for concept_root in concept_roots: print(f"ConceptRoot: {concept_root.entity}") for concept in concept_root.concepts(): total_tests += 1 print(f"Concept: {concept.name}") try: # Build rule tree from concept template if len(concept.template().rules) > 1: attribute_rules = list(concept.template().rules) rules_root = ifcopenshell.mvd.rule("EntityRule", concept_root.entity, attribute_rules) else: rules_root = concept.template().rules[0] successful_tests += 1 failed_instances = 0 # Validate each instance of the target entity type for inst in ifc_file.by_type(concept_root.entity): try: # Extract data according to rules data = mvd.extract_data(rules_root, inst) # Validate extracted data against concept requirements valid, output = mvd.validate_data(concept, data) if not valid: failed_instances += 1 print(f"[VALID]" if valid else f"{Fore.RED}[failure]{Style.RESET_ALL}", inst) print(output) except Exception as e: print(f"{Fore.RED}EXCEPTION: {e}{Style.RESET_ALL}", inst) print(f"{failed_instances} out of {len(ifc_file.by_type(concept_root.entity))} instances failed") print("-" * 40) except Exception as e: print(f"EXCEPTION: {Fore.RED}{e}{Style.RESET_ALL}") print("-" * 40) failed_tests = total_tests - successful_tests print(f"\nRESULTS OVERVIEW") print(f"Total tests: {total_tests}") print(f"Executed tests: {successful_tests}") print(f"Failed tests: {failed_tests}") ``` -------------------------------- ### Parse mvdXML Concept Roots using Python Source: https://context7.com/opensourcebim/python-mvdxml/llms.txt This Python snippet shows how to parse an mvdXML file using `ifcopenshell.mvd.concept_root.parse` to access `ConceptRoot` objects. It iterates through these roots to display their names and entities, and further accesses their applicability and associated concepts, including templates and rules. ```python from ifcopenshell.mvd import concept_root # Parse mvdXML file - yields concept_root objects concept_roots = list(concept_root.parse("model.mvdxml")) for cr in concept_roots: print(f"Concept Root: {cr.name}") print(f"Entity: {cr.entity}") # Access applicability filtering applicability = cr.applicability() print(f"Applicability: {applicability.name}") # Iterate through concepts for concept in cr.concepts(): print(f" Concept: {concept.name}") # Get template with parsed rules template = concept.template() print(f" Template Entity: {template.entity}") print(f" Rules: {len(template.rules)}") # Access concept rules (constraints/requirements) rules = concept.rules() print(f" Constraints: {rules}") ``` -------------------------------- ### Visualize IFC Validation Results in 3D using Python Source: https://context7.com/opensourcebim/python-mvdxml/llms.txt Visualizes IFC entities in a 3D view, color-coding them based on their mvdXML validation status. Non-compliant entities are shown in red, compliant entities in green, and other building elements in white, providing a clear visual representation of compliance. ```python import ifcopenshell from ifcopenshell.mvd import mvd mvd_concept = mvd.open_mvd("mvd_examples/wall_extraction.mvdxml") ifc_file = ifcopenshell.open("Duplex_A_20110505.ifc") all_data, verification_matrix = mvd.get_data(mvd_concept, ifc_file) non_respecting_entities = mvd.get_non_respecting_entities(ifc_file, verification_matrix) # Visualize with color coding: # - Red: non-compliant entities # - Green: compliant entities of target type # - White: other building elements mvd.visualize(ifc_file, non_respecting_entities) ``` -------------------------------- ### Visualize mvdXML Validation Results Source: https://github.com/opensourcebim/python-mvdxml/blob/master/README.md This snippet utilizes the mvd module to visualize the entities within an IFC file that either respect or do not respect the defined mvdXML concepts. This is useful for quickly identifying compliance issues. ```python # Visualize results mvd.visualize(file, non_respecting_entities) ``` -------------------------------- ### Validate IFC File Against mvdXML Concepts Source: https://github.com/opensourcebim/python-mvdxml/blob/master/README.md This comprehensive Python script performs validation of an IFC file against multiple mvdXML concepts. It iterates through concept roots and individual concepts, extracts relevant data based on rules, and reports validation status for each instance. It also provides a summary of test results. ```python import ifcopenshell from ifcopenshell.mvd import mvd from colorama import Fore from colorama import Style concept_roots = list(ifcopenshell.mvd.concept_root.parse(MVDXML_FILENAME)) file = ifcopenshell.open(IFC_FILENAME) tt = 0 # total number of tests ts = 0 # total number of successful tests for concept_root in concept_roots: print("ConceptRoot: ", concept_root.entity) for concept in concept_root.concepts(): tt = tt + 1 print("Concept: ", concept.name) try: if len(concept.template().rules) > 1: attribute_rules = [] for rule in concept.template().rules: attribute_rules.append(rule) rules_root = ifcopenshell.mvd.rule("EntityRule", concept_root.entity, attribute_rules) else: rules_root = concept.template().rules[0] ts = ts + 1 finst = 0 #failed instances for inst in file.by_type(concept_root.entity): try: data = mvd.extract_data(rules_root, inst) valid, output = mvd.validate_data(concept, data) if not valid: finst = finst + 1 print("[VALID]" if valid else Fore.RED +"[failure]"+Style.RESET_ALL, inst) print(output) except Exception as e: print(Fore.RED+"EXCEPTION: ", e, Style.RESET_ALL,inst) print () print (int(finst), "out of", int(len(file.by_type(concept_root.entity))), "instances failed the check") print ("---------------------------------") except Exception as e: print("EXCEPTION: "+Fore.RED,e,Style.RESET_ALL) print("---------------------------------") print("---------------------------------") print("---------------------------------") tf = tt-ts # total number of failed tests print ("\nRESULTS OVERVIEW") print ("Total number of tests: ",tt) print ("Total number of executed tests: ", ts) print ("Total number of failed tests: ", tf) ``` -------------------------------- ### Python: Export mvdXML Data to CSV and XLSX Source: https://context7.com/opensourcebim/python-mvdxml/llms.txt This Python script shows how to load an mvdXML definition and an IFC file, extract data based on mvdXML concepts, and export the extracted data into both XLSX and CSV spreadsheet formats. It uses the ifcopenshell library for IFC processing and its mvd module for data extraction and export. ```python import ifcopenshell from ifcopenshell.mvd import mvd # Load and process data mvd_concept = mvd.open_mvd("mvd_examples/wall_extraction.mvdxml") ifc_file = ifcopenshell.open("model.ifc") # Get concepts and entities concepts = list(mvd_concept.concepts()) entities = ifc_file.by_type(mvd_concept.entity) # Extract data for each concept all_data = [] for concept in concepts: rules_root = concept.template().rules[0] extracted = mvd.get_data_from_mvd(entities, rules_root, filtering=False) all_data.append(extracted) # Prepare data for export all_data = mvd.correct_for_export(all_data) # Export to XLSX (creates spreadsheet_output/filename.xlsx) mvd.export_to_xlsx("output.xlsx", concepts, all_data) # Export to CSV (creates spreadsheet_output/filename.csv) mvd.export_to_csv("output.csv", concepts, all_data) # Spreadsheet structure: # - First row: concept names # - Columns: one per concept # - Rows: one per entity instance # - Cells: extracted values or aggregated data ``` -------------------------------- ### Filter IFC Entities by mvdXML Compliance using Python Source: https://context7.com/opensourcebim/python-mvdxml/llms.txt Separates IFC entities into compliant and non-compliant groups based on mvdXML validation results. It allows for the creation of a new IFC file containing only the compliant entities, aiding in data cleanup and analysis. ```python import ifcopenshell from ifcopenshell.mvd import mvd mvd_concept = mvd.open_mvd("mvd_examples/wall_extraction.mvdxml") ifc_file = ifcopenshell.open("Duplex_A_20110505.ifc") all_data, verification_matrix = mvd.get_data(mvd_concept, ifc_file, spreadsheet_export=True) # Get entities that fail mvdXML validation non_respecting_entities = mvd.get_non_respecting_entities(ifc_file, verification_matrix) print(f"Found {len(non_respecting_entities)} non-compliant entities") # Get entities that pass mvdXML validation respecting_entities = mvd.get_respecting_entities(ifc_file, verification_matrix) print(f"Found {len(respecting_entities)} compliant entities") # Create new IFC file with only compliant entities new_file = ifcopenshell.file(schema=ifc_file.schema) proj = ifc_file.by_type("IfcProject")[0] new_file.add(proj) for entity in respecting_entities: new_file.add(entity) new_file.write("compliant_model.ifc") ``` -------------------------------- ### Extract IFC Data with mvdXML and Export to Spreadsheet using Python Source: https://context7.com/opensourcebim/python-mvdxml/llms.txt Extracts data from IFC files based on mvdXML concepts and automatically exports the extracted data to spreadsheet files (CSV/XLSX). This function returns the extracted data and a verification matrix indicating the pass/fail status for each concept. ```python import ifcopenshell from ifcopenshell.mvd import mvd # Load mvdXML and IFC file mvd_concept = mvd.open_mvd("mvd_examples/wall_extraction.mvdxml") ifc_file = ifcopenshell.open("Duplex_A_20110505.ifc") # Extract all data matching the mvdXML concept # Returns tuple: (all_data, verification_matrix) # all_data: list of dictionaries with extracted values # verification_matrix: dict mapping GlobalIds to pass/fail status per concept all_data, verification_matrix = mvd.get_data(mvd_concept, ifc_file, spreadsheet_export=True) # Spreadsheet files are created in spreadsheet_output/ directory # - output_filtered.xlsx/csv (if applicability filtering is used) # - output_non_filtered.xlsx/csv (if no filtering) ``` -------------------------------- ### Validate IFC Data Against mvdXML Concepts using Python Source: https://context7.com/opensourcebim/python-mvdxml/llms.txt Performs detailed validation of extracted IFC data against mvdXML concept rules. This function is crucial for ensuring data quality and adherence to specific industry standards defined in mvdXML. ```python import ifcopenshell from ifcopenshell.mvd import mvd from colorama import Fore, Style # Parse mvdXML and open IFC file concept_roots = list(ifcopenshell.mvd.concept_root.parse("model.mvdxml")) ifc_file = ifcopenshell.open("model.ifc") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.