[IfcOpenShell-Python] How to create a cone in an ifc file? i asked chatgtp

So i did a little test with chatgtp, how good it can help to code for ifc.

see here my chat with chatgtp https://refined-github-html-preview.kidonng.workers.dev/Martin15135215/git_ifc_test/raw/main/chatgtp_cone_ifcopenshell_python.html

It was not so good as i hoped, however i found out with chatgtp that there is indeed an ifc entity IfcRightCircularCone https://standards.buildingsmart.org/IFC/RELEASE/IFC4/ADD2_TC1/HTML/link/ifcrightcircularcone.htm

and then with a bit research with https://sourcegraph.com/ i found an ifc file that did use IfcRightCircularCone https://github.com/buildingSMART/Sample-Test-Files/blob/master/IFC 4.0/BuildingSMARTSpec/geographic-referencing.ifc

and also with this python script as inspiration https://github.com/IfcOpenShell/IfcOpenShell/blob/v0.7.0/src/blenderbim/scripts/obj2ifc.py

i think it is possible to create an python script that will create a cone

tdlr: with only chatgtp, i think that would be diffucult -> however that could maybe change in the future

Coen

Comments

  • I also asked chatGPT a question:

    Martin156131theoryshawGorgiousMoultAceNigel
  • edited February 2023

    Dear chapGPT, is there an easier way to make money? ;)

    I'm a noobie too, but the following seems to work...

    import bpy
    import ifcopenshell
    from ifcopenshell.api import run
    import numpy
    import bpy
    
    ifc_file = ifcopenshell.file()
    
    project = run("root.create_entity", ifc_file, ifc_class="IfcProject", name="Demo")
    
    run("unit.assign_unit", ifc_file, length={"is_metric": True, "raw": "METERS"})
    
    context = run("context.add_context", ifc_file, context_type="Model")
    body = run("context.add_context", ifc_file,context_type="Model", context_identifier="Body", target_view="MODEL_VIEW", parent=context)
    
    # Create a site, building, and storey. Many hierarchies are possible.
    site = run("root.create_entity", ifc_file, ifc_class="IfcSite", name="My Site")
    building = run("root.create_entity", ifc_file, ifc_class="IfcBuilding", name="Building A")
    storey = run("root.create_entity", ifc_file, ifc_class="IfcBuildingStorey", name="Ground Floor")
    
    # Since the site is our top level location, assign it to the project
    # Then place our building on the site, and our storey in the building
    run("aggregate.assign_object", ifc_file, relating_object=project, product=site)
    run("aggregate.assign_object", ifc_file, relating_object=site, product=building)
    run("aggregate.assign_object", ifc_file, relating_object=building, product=storey)
    
    elementproxy = ifcopenshell.api.run("root.create_entity", 
                                    ifc_file,
                                    ifc_class="IfcBuildingElementProxy", 
                                    predefined_type=None,
                                    name="testy"
                                    )
    
    position = ifc_file.createIfcAxis2Placement3D(ifc_file.createIfcCartesianPoint((1.0, 2.0, 3.0)))
    cone = ifc_file.create_entity("IfcRightCircularCone")
    cone.Height = 1000.0
    cone.BottomRadius = 500.0
    cone.Position = position
    
    representation = ifc_file.create_entity("IfcShapeRepresentation")
    representation.ContextOfItems = context
    representation.RepresentationIdentifier = "Body"
    representation.RepresentationType = "CSG"
    representation.Items = [cone]
    
    ifcopenshell.api.run("geometry.assign_representation", ifc_file, product=elementproxy, representation=representation)
    run("spatial.assign_container", ifc_file, relating_structure=storey, product=elementproxy)
    
    file_name="elementproxy"
    folder_path = "C://Users//Owner//Desktop"
    filename = str(file_name) + ".ifc"
    file_path = (folder_path + filename)
    
    ifc_file.write(file_path)
    
    Martin156131
  • edited February 2023

    @theoryshaw said:
    Dear chapGPT, is there an easier way to make money? ;)

    Haha, I only asked for a way to fund osarch ;-)

    Script creates a nice cone by the way :-)

    Martin156131
  • @Moult

    You're phrasing the question wrong ;-)

    MoultbrunopostletheoryshawCadGiruAceMartin156131NigelGorgioustlang
  • @theoryshaw
    With your script i successfully created a cone, and i could open it in BIMvision, however I could not open it in BlenderBIM :(

  • @theoryshaw i think IfcCsgSolid is missing in your script between IFCSHAPEREPRESENTATION and IFCRIGHTCIRCULARCONE
    I made this script

    import ifcopenshell
    import ifcopenshell.api
    import ifcopenshell.api.owner.settings
    version = "IFC4"
    basename = "My Project"
    file = ifcopenshell.api.run("project.create_file", version=version)
    person = ifcopenshell.api.run("owner.add_person", file)
    person[0] = person.GivenName = None
    person.FamilyName = "user"
    org = ifcopenshell.api.run("owner.add_organisation", file)
    org[0] = None
    org.Name = "template"
    user = ifcopenshell.api.run("owner.add_person_and_organisation", file, person=person, organisation=org)
    application = ifcopenshell.api.run("owner.add_application", file)
    ifcopenshell.api.owner.settings.get_user = lambda ifc: user
    ifcopenshell.api.owner.settings.get_application = lambda ifc: application
    
    project = ifcopenshell.api.run("root.create_entity", file, ifc_class="IfcProject", name=basename)
    lengthunit = ifcopenshell.api.run("unit.add_si_unit", file, unit_type="LENGTHUNIT", name="METRE")
    ifcopenshell.api.run("unit.assign_unit", file, units=[lengthunit])
    model = ifcopenshell.api.run("context.add_context", file, context_type="Model")
    context = ifcopenshell.api.run(
        "context.add_context",
        file,
        context_type="Model",
        context_identifier="Body",
        target_view="MODEL_VIEW",
        parent=model,
    )
    site = ifcopenshell.api.run("root.create_entity", file, ifc_class="IfcSite", name="My Site")
    building = ifcopenshell.api.run("root.create_entity", file, ifc_class="IfcBuilding", name="My Building")
    storey = ifcopenshell.api.run(
        "root.create_entity", file, ifc_class="IfcBuildingStorey", name="My Storey"
    )
    ifcopenshell.api.run("aggregate.assign_object", file, product=site, relating_object=project)
    ifcopenshell.api.run("aggregate.assign_object", file, product=building, relating_object=site)
    ifcopenshell.api.run("aggregate.assign_object", file, product=storey, relating_object=building)
    
    origin = file.createIfcAxis2Placement3D(
        file.createIfcCartesianPoint((0.0, 0.0, 0.0)),
        file.createIfcDirection((0.0, 0.0, 1.0)),
        file.createIfcDirection((1.0, 0.0, 0.0)),
    )
    
    history = ifcopenshell.api.run("owner.create_owner_history", file)
    
    cone_rep = file.create_entity("IfcRightCircularCone", Position=origin, Height=0.5 , BottomRadius=0.15)
    cone = file.create_entity("IfcCsgSolid", TreeRootExpression=cone_rep )
    representation = file.createIfcProductDefinitionShape(
                    None,
                    None,
                    [
                        file.createIfcShapeRepresentation(
                            context,
                            "Body",
                            "CSG",
                            [cone],
                        )
                    ],
                )
    product = file.create_entity(
                    "IfcBuildingElementProxy",
                    **{
                        "GlobalId": ifcopenshell.guid.new(),
                        "Name": "Cone",
                        "ObjectPlacement": None,
                        "Representation": representation,
                    }
                )
    ifcopenshell.api.run("spatial.assign_container", file, product=product, relating_structure=storey)
    
    file.write("cone.ifc")
    
    

    i could then open this ifc file in BlenderBIM

    Coen
  • @Martin156131

    Nice! Are you trying to script an IfcGardenGnome? ;-)

    Nigel
  • edited February 2023

    @Coen
    No no ? much simpler, I am trying to create an arrow

    import ifcopenshell
    import ifcopenshell.api
    import ifcopenshell.api.owner.settings
    version = "IFC4"
    basename = "My Project"
    file = ifcopenshell.api.run("project.create_file", version=version)
    person = ifcopenshell.api.run("owner.add_person", file)
    person[0] = person.GivenName = None
    person.FamilyName = "user"
    org = ifcopenshell.api.run("owner.add_organisation", file)
    org[0] = None
    org.Name = "template"
    user = ifcopenshell.api.run("owner.add_person_and_organisation", file, person=person, organisation=org)
    application = ifcopenshell.api.run("owner.add_application", file)
    ifcopenshell.api.owner.settings.get_user = lambda ifc: user
    ifcopenshell.api.owner.settings.get_application = lambda ifc: application
    
    project = ifcopenshell.api.run("root.create_entity", file, ifc_class="IfcProject", name=basename)
    lengthunit = ifcopenshell.api.run("unit.add_si_unit", file, unit_type="LENGTHUNIT", name="METRE")
    ifcopenshell.api.run("unit.assign_unit", file, units=[lengthunit])
    model = ifcopenshell.api.run("context.add_context", file, context_type="Model")
    context = ifcopenshell.api.run(
        "context.add_context",
        file,
        context_type="Model",
        context_identifier="Body",
        target_view="MODEL_VIEW",
        parent=model,
    )
    site = ifcopenshell.api.run("root.create_entity", file, ifc_class="IfcSite", name="My Site")
    building = ifcopenshell.api.run("root.create_entity", file, ifc_class="IfcBuilding", name="My Building")
    storey = ifcopenshell.api.run(
        "root.create_entity", file, ifc_class="IfcBuildingStorey", name="My Storey"
    )
    ifcopenshell.api.run("aggregate.assign_object", file, product=site, relating_object=project)
    ifcopenshell.api.run("aggregate.assign_object", file, product=building, relating_object=site)
    ifcopenshell.api.run("aggregate.assign_object", file, product=storey, relating_object=building)
    
    origin = file.createIfcAxis2Placement3D(
        file.createIfcCartesianPoint((1.0, 1.0, 0.0)),
        file.createIfcDirection((0.0, 0.0, 1.0)),
        file.createIfcDirection((1.0, 0.0, 0.0)),
    )
    placement = file.createIfcLocalPlacement(None, origin)
    history = ifcopenshell.api.run("owner.create_owner_history", file)
    height = 0.5
    origin_cone = file.createIfcAxis2Placement3D(
        file.createIfcCartesianPoint((0.0, 0.0, height)),
        file.createIfcDirection((0.0, 0.0, -1.0)),
        file.createIfcDirection((-1.0, 0.0, 0.0)),
    )
    cone_rep = file.create_entity("IfcRightCircularCone", Position=origin_cone, Height=height , BottomRadius=0.15)
    cone = file.create_entity("IfcCsgSolid", TreeRootExpression=cone_rep )
    
    origin_cylinder = file.createIfcAxis2Placement3D(
        file.createIfcCartesianPoint((0.0, 0.0, height)),
        file.createIfcDirection((0.0, 0.0, 1.0)),
        file.createIfcDirection((1.0, 0.0, 0.0)),
    )
    
    cy_rep = file.create_entity("IfcRightCircularCylinder", Position=origin_cylinder, Height=height , Radius=0.05)
    cylinder = file.create_entity("IfcCsgSolid", TreeRootExpression=cy_rep )
    
    
    representation = file.createIfcProductDefinitionShape(
                    None,
                    None,
                    [
                        file.createIfcShapeRepresentation(
                            context,
                            "Body",
                            "CSG",
                            [cone, cylinder],
                        )
                    ],
                )
    product = file.create_entity(
                    "IfcBuildingElementProxy",
                    **{
                        "GlobalId": ifcopenshell.guid.new(),
                        "Name": "Arrow",
                        "ObjectPlacement": placement,
                        "Representation": representation,
                    }
                )
    ifcopenshell.api.run("spatial.assign_container", file, product=product, relating_structure=storey)
    
    file.write("arrow.ifc")
    
    Coen
  • Is it a virtual penetrometer?

  • No, that's not what I had in mind. I rather think that if you measure points outside, for example, you also want to show this in the IFC model. These arrows are there to visualize the measured points appropriately in the model. There are certainly other applications for these arrows, but that was my thought :)

Sign In or Register to comment.