If you're reading this, we've just migrated servers! If anything looks broken please email dion@thinkmoult.com :)

IfcOS BCF parser, Get a list of viewpoints objects GUIDs by Python

edited June 2021 in General

I would like to get started hacking on the new BCF parser.
Assumed I have a bcf and latest IfcOpenShell. Does someone has a code snipped to get a list of viewpoint objects GUIDs by Python?
cheers bernd

Comments

  • I think it's here: https://github.com/IfcOpenShell/IfcOpenShell/blob/v0.6.0/src/bcf/bcf/v3/bcfxml.py#L657
    You could find example to read bcf file here: https://github.com/IfcOpenShell/IfcOpenShell/tree/v0.6.0/src/bcf
    Some untested lines:

    from bcf import bcfxml
    
    bcf = bcfxml.load("/path/to/file.bcf")
    for topic in bcf.get_topics():
        viewpoints = bcf.get_viewpoints(topic.guid)
        for viewpoint in viewpoints:
            print(viewpoint.guid)
    
  • Just tested. Please note that bcf lib only supports version 2.1 and 3.0.

    from bcf import bcfxml
    
    bcf = bcfxml.load("/path/to/file.bcf")
    for topic_guid in bcf.get_topics():
        viewpoints = bcf.get_viewpoints(topic_guid )
        for viewpoint_guid in viewpoints:
            print(viewpoint_guid)
    

    I think get_topics should return a list of Topic objects and get_viewpoints should return a list of Viewpoint objects. That sounds more pythonic. What do you think @Moult ?

  • edited June 2021

    thanks very much ... I had to adapt it a bit to get it working.

    import sys
    sys.path.append("C:/Users/BHA/Desktop/bcftest/bcf")
    
    from bcf import bcfxml
    bcf = bcfxml.load("C:/Users/BHA/Desktop/bcftest/Issues_2021-06-24_20.26.bcf")
    
    for topic_guid, topic_obj in bcf.get_topics().items():
        print(topic_obj.title)
        viewpoints = bcf.get_viewpoints(topic_obj.guid)
        for viewpoint_guid, viewpoint_obj in viewpoints.items():
            print(viewpoint_obj.guid)
    

    this is what it came out ... :-)

    >>> import sys
    >>> sys.path.append("C:/Users/BHA/Desktop/bcftest/bcf")
    >>> 
    >>> from bcf import bcfxml
    >>> bcf = bcfxml.load("C:/Users/BHA/Desktop/bcftest/Issues_2021-06-24_20.26.bcf")
    >>> 
    >>> for topic_guid, topic_obj in bcf.get_topics().items():
    ...     print(topic_obj.title)
    ...     viewpoints = bcf.get_viewpoints(topic_obj.guid)
    ...     for viewpoint_guid, viewpoint_obj in viewpoints.items():
    ...         print(viewpoint_obj.guid)
    ... 
    test_2
    8eb860dd-735e-45ac-b368-8975382a2da4
    test_4
    4611f220-647f-4d56-b196-d355c39e44a6
    test_1
    d77374e9-97a1-4c03-9a42-9254cd432798
    test_5
    4ab0b1f5-ade5-46ff-a376-be2eb4770ca7
    Decke UG inkl Rampe
    841b257c-dc37-458a-bc6a-d0dddb2074d4
    test_3
    82c10923-da08-478a-8a0f-286ca8446108
    c978fb25-f881-4ef5-9c15-19edf842a638
    >>> 
    
  • import sys
    sys.path.append("C:/Users/BHA/Desktop/bcftest/bcf")
    
    from bcf import bcfxml
    bcf = bcfxml.load("C:/Users/BHA/Desktop/bcftest/Issues_2021-06-24_20.26.bcf")
    
    the_topic = None
    for topic_guid, topic_obj in bcf.get_topics().items():
        print(topic_obj.title)
        if topic_obj.title == "test_2":
            the_topic = topic_obj
            break
    
    print(the_topic.title)
    viewpoints = bcf.get_viewpoints(the_topic.guid)
    for viewpoint_guid, viewpoint_obj in viewpoints.items():
        for component in viewpoint_obj.components.visibility.exceptions:
            print("    {}".format(component.ifc_guid))
    
    
    >>> import sys
    >>> sys.path.append("C:/Users/BHA/Desktop/bcftest/bcf")
    >>> 
    >>> from bcf import bcfxml
    >>> bcf = bcfxml.load("C:/Users/BHA/Desktop/bcftest/Issues_2021-06-24_20.26.bcf")
    >>> 
    >>> the_topic = None
    >>> for topic_guid, topic_obj in bcf.get_topics().items():
    ...     print(topic_obj.title)
    ...     if topic_obj.title == "test_2":
    ...         the_topic = topic_obj
    ...         break
    ... 
    test_2
    >>> print(the_topic.title)
    test_2
    >>> viewpoints = bcf.get_viewpoints(the_topic.guid)
    >>> for viewpoint_guid, viewpoint_obj in viewpoints.items():
    ...     for component in viewpoint_obj.components.visibility.exceptions:
    ...         print("    {}".format(component.ifc_guid))
    ... 
        00kS9D1zD0ufeqXLHcYjOn
        1GIMuf0qz5qPFOfK7KZl9X
        1JDOfXAI96V8EMm0S6yv3L
        2joDpfilbB4w6j9gqHGMX7
    >>> 
    
  • @htlcnn said:
    ```
    I think get_topics should return a list of Topic objects and get_viewpoints should return a list of Viewpoint objects. That sounds more pythonic. What do you think @Moult ?

    a list of the objects would make things easier, thats True

  • Isn't a dictionary better than a list? In a list you need to sort through all to find the topic you want, whereas a dictionary (like it currently returns) you can find the topic by its ID, or run through its .values() as a list if you wished?

    Glad to see it works :)

  • @Moult said:
    ... run through its .values() ...

    Ahh yes this would have been easier than what I have used.

    Mhh most other parser libraries I know use list in such circumstances. For playing around a list is way simpler. Just assign the first item [0] to some variable and play around in python shell.

    Eventually it does not matter. As htlcnn said a list just feels more pythonic.

    BTW:
    great stuff this bcf library, will need and use it to read some information out of bcfs.

  • It would be awesome to replace the current FreeCAD bcfplugin lib with this one, since this one fixes many of the issues the bcfplugin had, and is updated for the latest BCF specs.

  • edited July 2021

    @Moult said:
    It would be awesome to replace the current FreeCAD bcfplugin lib with this one, since this one fixes many of the issues the bcfplugin had, and is updated for the latest BCF specs.

    yes for sure it makes sense to use your new parser. JFYI ... ATM this is not near the top of my OSS AEC TODO ... I just do not need it. There is BIMTester which is much more near the TOP and does not get done either ... I still use the version befor your big changes ... but on a daily basis ... :-)

    Moult
  • are there some code examples around to create a new bcf from scratch with the ifcopenshell bcf paraser?

    bernd

  • Try this:

    bcfxml = bcf.v2.bcfxml.BcfXml()
    bcfxml.new_project()
    
  • edited April 10

    it seams there has been taken place some API change ... following the code which works with latest BBIM for me to get main information out of the bcf ...

    from bcf import bcfxml
    bcf = bcfxml.load("C:/Users/BHA/Desktop/test.bbbim_zoom3_extended.bcf")
    for topic_guid, topic_obj in bcf.get_topics().items():
        print("{}: {}".format(topic_guid, topic_obj.topic.title))
        for viewpoint_guid, viewpoint_obj in topic_obj.viewpoints.items():
            print("  {}".format(viewpoint_obj.guid))
    

    inside BBIM ...

    >>> for topic_guid, topic_obj in bcf.get_topics().items():
    ...     print("{}: {}".format(topic_guid, topic_obj.topic.title))
    ...     for viewpoint_guid, viewpoint_obj in topic_obj.viewpoints.items():
    ...         print("  {}".format(viewpoint_obj.guid))
    ...         
    2ff81b84-6ce6-4607-8d7c-35a7f7714f96: mein zweiter issue
      f47abc58-465a-4a8c-aa25-d44abc94c0d7
      124cb5d4-ce47-455e-9938-3fc0a2a68a41
      9a36adef-b285-4673-a373-c7c03601b5a9
      c9c71e3e-b5ac-4a3b-9da6-480a914b5873
    81321a8d-8909-45f0-9982-edd0d116a533: myissue
      b5ccadb4-af11-4c1b-84df-da9c1a747075
    
    >>> 
    
  • some more viewpoint information ...

    for topic_guid, topic_obj in bcf.get_topics().items():
        print("{}: {}".format(topic_guid, topic_obj.topic.title))
        for viewpoint_guid, viewpoint_obj in topic_obj.viewpoints.items():
            print("  {}".format(viewpoint_obj.guid))
            comp = viewpoint_obj.visualization_info.components
            # print(comp)
            if comp is not None:
                print("    {}".format(comp.view_setup_hints))
                print("    {}".format(comp.selection))
                print("    {}".format(comp.visibility))
                #print("    {}".format(comp.visibility.exceptions))
                #print("    {}".format(comp.visibility.default_visibility))
                print("    {}".format(comp.coloring))
            else:
                print("    {}".format(comp))
    
  • even a few more view point information ...

    for topic_guid, topic_obj in bcf.get_topics().items():
        print("{}: {}".format(topic_guid, topic_obj.topic.title))
        for viewpoint_guid, viewpoint_obj in topic_obj.viewpoints.items():
            print("  {}".format(viewpoint_obj.guid))
            comp = viewpoint_obj.visualization_info.components
            # print(comp)
            if comp is not None:
                #
                print("    view_setup_hints")
                print("      {}".format(comp.view_setup_hints))
                #
                print("    visibility")
                if comp.visibility is None:
                    print("      {}".format(comp.visibility))
                else:
                    print("      {}".format(comp.visibility.default_visibility))
                    if hasattr(comp.visibility.exceptions, "component"):
                        print("      len   exceptions {}".format(len(comp.visibility.exceptions.component)))
                        print("      first exceptions {}".format(comp.visibility.exceptions.component[0]))
                #
                print("    selection")
                if comp.selection is None:
                    print("      {}".format(comp.selection))
                else:
                    print("      {}".format(comp.selection.component))
                #
                print("    coloring")
                if comp.coloring is None:
                    print("      {}".format(comp.coloring))
                else:
                    # print("      {}".format(comp.coloring.color))
                    print("      len  coloring: {}".format(len(comp.coloring.color)))
                    print("      first coloring: {}".format(comp.coloring.color[0].component))
                    print("      color coloring: {}".format(comp.coloring.color[0].color))
            else:
                print("    {}".format(comp))
            print("\n\n\n")
    
Sign In or Register to comment.