import ifcopenshell import ifcopenshell.api import rhino3dm rhinoPath = r"C:\Users\sondr\OneDrive\Documents\ifcExamples\sphereExample.3dm" #rhinoPath = r"C:\Users\sondr\OneDrive\Documents\ifcExamples\foundationExample.3dm" ifcPath = r"C:\Users\sondr\OneDrive\Documents\ifcExamples\newTest.ifc" #-------------- Create the IFC template ------------------------ O = 0., 0., 0.; X = 1., 0., 0.; Y = 0., 1., 0.; Z = 0., 0., 1. create_guid = lambda: ifcopenshell.guid.compress(uuid.uuid1().hex) def create_ifclocalplacement(f, point=O, dir1=Z, dir2=X, relative_to=None): axis2placement = create_ifcaxis2placement(f, point, dir1, dir2) ifclocalplacement2 = f.createIfcLocalPlacement(relative_to, axis2placement) return ifclocalplacement2 def create_ifcaxis2placement(f, point=O, dir1=Z, dir2=X): point = f.createIfcCartesianPoint(point) dir1 = f.createIfcDirection(dir1) dir2 = f.createIfcDirection(dir2) axis2placement = f.createIfcAxis2Placement3D(point, dir1, dir2) return axis2placement ifc_file = ifcopenshell.api.run("project.create_file", version="IFC4") person = ifcopenshell.api.run("owner.add_person", ifc_file, identification="SBE", family_name="SBE@AAJ", given_name="SBE") org = ifcopenshell.api.run("owner.add_organisation", ifc_file, identification="AAJ", name="AAJ") user = ifcopenshell.api.run("owner.add_person_and_organisation", ifc_file, person=person, organisation=org) application = ifcopenshell.api.run("owner.add_application", ifc_file) project = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcProject", name="TEST") lengthunit = ifcopenshell.api.run("unit.add_si_unit", ifc_file, unit_type="LENGTHUNIT", name="METRE") unitAssignment = ifcopenshell.api.run("unit.assign_unit", ifc_file, units=[lengthunit]) model = ifcopenshell.api.run("context.add_context", ifc_file, context_type="Model") context = ifcopenshell.api.run("context.add_context", ifc_file, context_type="Model", context_identifier="Body", target_view="MODEL_VIEW",parent=model,) site = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcSite", name="My Site") building = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcBuilding", name="My Building") storey = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcBuildingStorey", name="My Storey") aggregation1 = ifcopenshell.api.run("aggregate.assign_object", ifc_file, product=site, relating_object=project) aggregation2 = ifcopenshell.api.run("aggregate.assign_object", ifc_file, product=building, relating_object=site) aggregation3 = ifcopenshell.api.run("aggregate.assign_object", ifc_file, product=storey, relating_object=building) origin = ifc_file.createIfcAxis2Placement3D(ifc_file.createIfcCartesianPoint((0.0, 0.0, 0.0)), ifc_file.createIfcDirection((0.0, 0.0, 1.0)),ifc_file.createIfcDirection((1.0, 0.0, 0.0)),) placement = ifc_file.createIfcLocalPlacement(None, origin) history = ifcopenshell.api.run("owner.create_owner_history", ifc_file) #---------------------------------------------------------------------------------- ## Create entities using either (root.create_entity adds GUID and ownerhistory, use that one for rooted instances) #ifc_file.create_entity("IfcWall") #ifc_file.createIfcWall() #ifcopenshell.api.run("root.create_entity", "IfcWall") # Add a NURBS sphere rhino_file = rhino3dm.File3dm.Read(rhinoPath) sphere = rhino_file.Objects[0] sphereBrep = sphere.Geometry surf=sphereBrep.Surfaces[0] face=sphereBrep.Faces[0] nurbsSurf = surf.ToNurbsSurface() isClosedU = nurbsSurf.IsClosed(0); isClosedV = nurbsSurf.IsClosed(1) nurbsSurfDegreeU = nurbsSurf.Degree(0); nurbsSurfDegreeV = nurbsSurf.Degree(1) nurbsSurfOrderU = nurbsSurf.OrderU; nurbsSurfOrderV = nurbsSurf.OrderV nurbsSurfKnotsU = nurbsSurf.KnotsU; nurbsSurfKnotsV = nurbsSurf.KnotsV knotsU = nurbsSurfKnotsU.ToList(); knotsV = nurbsSurfKnotsV.ToList() def getKnotMultiplicity(knots): knotMult = [] for i in range(len(knots)): knotMult.append(nurbsSurfKnotsU.KnotMultiplicity(i)) return knotMult knotMultU = getKnotMultiplicity(knotsU); knotMultV = getKnotMultiplicity(knotsV); nurbsSurfControlPoints = nurbsSurf.Points points = [] for coordU in range(nurbsSurfControlPoints.CountU): pnts = [] for coordV in range(nurbsSurfControlPoints.CountV): pnt = nurbsSurfControlPoints[coordU,coordV] pnts.append([pnt.X, pnt.Y, pnt.Z]) points.append(pnts) # Create an attribute dictionary: attributes = { "UDegree":nurbsSurfDegreeU, "VDegree":nurbsSurfDegreeV, "UClosed":isClosedU, "VClosed":isClosedV, "UMultiplicities":knotMultU, "VMultiplicities":knotMultV, "UKnots":knotsU, "VKnots":knotsV, } #-------------- Create the corresponding IFC geometry ------------------------ # Create IfcCartesian points for the control points: def createIfcCartesianPoint(X,Y,Z): return ifc_file.createIfcCartesianPoint((X,Y,Z)) def controlPoints(points): attributes = dict() controlPointsList = [] for list_ in points: tempList = tuple() for pnt in list_: ifcPoint = ifc_file.createIfcCartesianPoint((pnt[0], pnt[1], pnt[2])) tempList += (ifcPoint,) controlPointsList.append(tempList) attributes["ControlPointsList"] = controlPointsList return attributes attributes = {**attributes, **controlPoints(points)} bSurf = ifc_file.create_entity("IfcBSplineSurfaceWithKnots", **attributes) # Edge def controlPointsEdge(points): attributes = dict() controlPointsList = [] for pnt in points: ifcPoint = ifc_file.createIfcCartesianPoint((pnt[0], pnt[1], pnt[2])) controlPointsList.append(ifcPoint) attributes["ControlPointsList"] = controlPointsList return attributes brepEdge = sphereBrep.Edges[0] nurbsEdge = brepEdge.ToNurbsCurve() nurbsEdgeControlPoints = nurbsEdge.Points points = [] for pnt in nurbsEdgeControlPoints: points.append([pnt.X, pnt.Y, pnt.Z]) attributes = controlPointsEdge(points) attributes["Knots"] = nurbsEdge.Knots.ToList() attributes["KnotMultiplicities"] = getKnotMultiplicity(nurbsEdge.Knots.ToList()) attributes["Degree"] = nurbsEdge.Degree attributes["ClosedCurve"] = nurbsEdge.IsClosed bEdge = ifc_file.create_entity("IfcBSplineCurveWithKnots", **attributes) pntStart = nurbsEdge.PointAtStart; pntEnd = nurbsEdge.PointAtEnd; vertexStart = ifc_file.createIfcVertexPoint(VertexGeometry=createIfcCartesianPoint(pntStart.X, pntStart.Y, pntStart.Z)) vertexEnd = ifc_file.createIfcVertexPoint(VertexGeometry=createIfcCartesianPoint(pntEnd.X, pntEnd.Y, pntEnd.Z)) edgeCurve = ifc_file.create_entity("IfcEdgeCurve", **{ "EdgeStart":vertexStart, "EdgeEnd":vertexEnd, "EdgeGeometry":bEdge, "SameSense":True}) orientedEdge = ifc_file.create_entity("IfcOrientedEdge", EdgeElement=edgeCurve) edgeLoop = ifc_file.create_entity("IfcEdgeLoop", EdgeList=[orientedEdge]) faceOuterBound = ifc_file.create_entity("IfcFaceOuterBound",Bound=edgeLoop) advancedFace = ifc_file.create_entity("IfcAdvancedFace", Bounds=(faceOuterBound,), FaceSurface=bSurf) closedShell = ifc_file.create_entity("IfcClosedShell", CfsFaces=[advancedFace]) advancedBrep = ifc_file.create_entity("IfcAdvancedBrep", Outer=closedShell) # Assign the advanced brep representation to an instanced entity: proxy = ifcopenshell.api.run("root.create_entity", ifc_file) shapeRep = ifc_file.createIfcShapeRepresentation(ContextOfItems=context, RepresentationIdentifier='Body', RepresentationType='AdvancedBrep', Items=[advancedBrep]) ifcopenshell.api.run("geometry.assign_representation", ifc_file, product=proxy, representation=shapeRep) containment = ifcopenshell.api.run("spatial.assign_container", ifc_file, product=proxy, relating_structure=storey) # Add a box: indexedPolygonalFace1 = ifc_file.createIfcIndexedPolygonalFace((1,2,3,4)) indexedPolygonalFace2 = ifc_file.createIfcIndexedPolygonalFace((6,2,3,7)) indexedPolygonalFace3 = ifc_file.createIfcIndexedPolygonalFace((7,3,4,8)) indexedPolygonalFace4 = ifc_file.createIfcIndexedPolygonalFace((8,4,1,5)) indexedPolygonalFace5 = ifc_file.createIfcIndexedPolygonalFace((1,4,3,2)) indexedPolygonalFace6 = ifc_file.createIfcIndexedPolygonalFace((6,7,8,5)) cartesianPointList3d = ifc_file.createIfcCartesianPointList3D(CoordList=((0.,0.,0.),(1.,0.,0.),(1.,1.,0.),(0.,1.,0.),(0.,0.,2.),(1.,0.,2.),(1.,1.,2.),(0.,1.,2.))) polygonalFaceSet = ifc_file.createIfcPolygonalFaceSet(Faces=[indexedPolygonalFace1, indexedPolygonalFace2, indexedPolygonalFace3, indexedPolygonalFace4, indexedPolygonalFace5, indexedPolygonalFace6]) proxy2 = ifcopenshell.api.run("root.create_entity", ifc_file, "IfcExtrudedAreaSolid") shapeRep = ifc_file.createIfcShapeRepresentation(ContextOfItems=context, RepresentationIdentifier='Body', RepresentationType='Tesselation', Items=[polygonalFaceSet,]) ifcopenshell.api.run("geometry.assign_representation", ifc_file, product=proxy2, representation=shapeRep) containment = ifcopenshell.api.run("spatial.assign_container", ifc_file, product=proxy2, relating_structure=storey) # Add rebars, this is the only geometry that works... ifcRebar = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcReinforcingBar") ifcRebarType= ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcReinforcingBarType") ifcopenshell.api.run("type.assign_type", ifc_file, related_object=ifcRebar, relating_type=ifcRebarType) pnt1 = ifc_file.createIfcCartesianPoint( (0.0,0.0,0.0) );pnt2 = ifc_file.createIfcCartesianPoint( (0.0,0.0,1.0) );pnt3 = ifc_file.createIfcCartesianPoint( (0.0,1.0,1.0) );pnt4 = ifc_file.createIfcCartesianPoint( (0.0,1.0,0.0) ) bar_line= ifc_file.createIfcPolyline([pnt1,pnt2,pnt3,pnt4]) bar_solid= ifc_file.createIfcSweptDiskSolidPolygonal(Directrix=bar_line, FilletRadius=250./1e3, Radius=6./1e3, InnerRadius=None) shapeRep= ifc_file.createIfcShapeRepresentation(ContextOfItems=context, RepresentationIdentifier='Body', RepresentationType='AdvancedSweptSolid', Items=[bar_solid]) ifcopenshell.api.run("geometry.assign_representation", ifc_file, product=ifcRebarType, representation=shapeRep) Axis1=ifc_file.createIfcDirection((1.,0.,0.)); Axis2=ifc_file.createIfcDirection((0.,1.,0.)); Axis3=ifc_file.createIfcDirection((0.,0.,1.)); Scale=1.0 maps = [] for i in range(10): map = ifc_file.createIfcMappedItem() map.MappingSource = ifcRebarType.RepresentationMaps[0] T = ifc_file.createIfcCartesianTransformationOperator3D() LocalOrigin=ifc_file.createIfcCartesianPoint((i*100./1e3,0.,0.)) ifcopenshell.api.run("attribute.edit_attributes", ifc_file, product=T, attributes={"Axis1":Axis1, "Axis2":Axis2, "Axis3":Axis3, "Scale":Scale, "LocalOrigin":LocalOrigin}) map.MappingTarget = T maps.append(map) new = list( ifcRebar.Representation.Representations[0].Items)+maps ifcRebar.Representation.Representations[0].Items = new rel = ifcopenshell.api.run("spatial.assign_container", ifc_file, product=ifcRebar, relating_structure=storey) # Write the geometry to ifc file: ifc_file.write(ifcPath)