import sys , os import time import uuid import math import ifcopenshell 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) timestamp = time.time() timestring = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(timestamp)) creator = "sbeAAJ" organization = "AAJ" application, application_version = "IfcOpenShell", "0.5" project_globalid, project_name = create_guid(), "TestGrid" # open for Blank ifc_file = ifcopenshell.file() Pnt_O = ifc_file.createIfcCartesianPoint( O ) axis_X = ifc_file.createIfcDirection( X ) axis_Y = ifc_file.createIfcDirection( Y ) axis_Z = ifc_file.createIfcDirection( Z ) #============================================= org = ifc_file.createIfcOrganization( ) org.Name = organization app = ifc_file.createIfcApplication( ) app.ApplicationDeveloper = org app.Version = "0.16.6700" app.ApplicationFullName = "FreeCAD" person = ifc_file.createIfcPerson() person.FamilyName="sbeAAJ" person_org= ifc_file.createIfcPersonAndOrganization() person_org.ThePerson=person person_org.TheOrganization=org owner_hist= ifc_file.createIfcOwnerHistory() owner_hist.OwningUser = person_org owner_hist.OwningApplication = app owner_hist.ChangeAction= "NOCHANGE" owner_hist.CreationDate= int(time.time()) LengthUnit = ifc_file.createIfcSIUnit() LengthUnit.UnitType = "LENGTHUNIT" LengthUnit.Prefix = "MILLI" LengthUnit.Name="METRE" AreaUnit = ifc_file.createIfcSIUnit() AreaUnit.UnitType = "AREAUNIT" AreaUnit.Name="SQUARE_METRE" VolumeUnit = ifc_file.createIfcSIUnit() VolumeUnit.UnitType = "VOLUMEUNIT" VolumeUnit.Name="CUBIC_METRE" PlaneAngleUnit = ifc_file.createIfcSIUnit() PlaneAngleUnit.UnitType = "PLANEANGLEUNIT" PlaneAngleUnit.Name ="RADIAN" AngleUnit = ifc_file.createIfcMeasureWithUnit() AngleUnit.UnitComponent =PlaneAngleUnit AngleUnit.ValueComponent = ifc_file.createIfcPlaneAngleMeasure(math.pi/180) DimExp = ifc_file.createIfcDimensionalExponents(0,0,0,0,0,0,0) ConvertBaseUnit = ifc_file.createIfcConversionBasedUnit() ConvertBaseUnit.Dimensions = DimExp ConvertBaseUnit.UnitType="PLANEANGLEUNIT" ConvertBaseUnit.Name="DEGREE" ConvertBaseUnit.ConversionFactor = AngleUnit UnitAssignment=ifc_file.createIfcUnitAssignment([LengthUnit , AreaUnit , VolumeUnit ,ConvertBaseUnit]) # Defining project and representation contexts WorldCoordinateSystem = ifc_file.createIfcAxis2Placement3D() WorldCoordinateSystem.Location=Pnt_O WorldCoordinateSystem.Axis = axis_Z WorldCoordinateSystem.RefDirection = axis_X context = ifc_file.createIfcGeometricRepresentationContext() context.ContextType = "Model" context.CoordinateSpaceDimension = 3 context.Precision = 1.e-05 context.WorldCoordinateSystem = WorldCoordinateSystem myProject = ifc_file.createIfcProject(create_guid()) myProject.OwnerHistory = owner_hist myProject.Name = "Test Rebar" myProject.RepresentationContexts = [context] myProject.UnitsInContext = UnitAssignment # Defining site, building and first story ------------ site_placement = ifc_file.createIfcLocalPlacement() site_placement.RelativePlacement=WorldCoordinateSystem mySite = ifc_file.createIfcSite( create_guid() ) mySite.OwnerHistory = owner_hist mySite.Name = "My Site" mySite.ObjectPlacement = site_placement mySite.CompositionType="ELEMENT" building_placement = ifc_file.createIfcLocalPlacement() building_placement.PlacementRelTo = site_placement building_placement.RelativePlacement = WorldCoordinateSystem myBuilding = ifc_file.createIfcBuilding( create_guid(), owner_hist ) myBuilding.Name = "Test Building" myBuilding.ObjectPlacement = building_placement myBuilding.CompositionType="ELEMENT" floor1_placement = ifc_file.createIfcLocalPlacement() floor1_placement.PlacementRelTo = building_placement floor1_placement.RelativePlacement = WorldCoordinateSystem floor1 = ifc_file.createIfcBuildingStorey( create_guid(), owner_hist ) floor1.Name = "Floor 1" floor1.ObjectPlacement = floor1_placement floor1.CompositionType="ELEMENT" container_project = ifc_file.createIfcRelAggregates(create_guid() , owner_hist) container_project.Name="Project Container" container_project.RelatingObject = myProject container_project.RelatedObjects = [mySite] container_site = ifc_file.createIfcRelAggregates(create_guid() , owner_hist) container_site.Name = "Site Container" container_site.RelatingObject = mySite container_site.RelatedObjects = [myBuilding] container_storey = ifc_file.createIfcRelAggregates(create_guid() , owner_hist) container_storey.Name = "Building Container" container_storey.RelatingObject = myBuilding container_storey.RelatedObjects = [floor1] # End Basic IFC #=========================================== 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 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,1000.0) );pnt3 = ifc_file.createIfcCartesianPoint( (0.0,1000.0,1000.0) );pnt4 = ifc_file.createIfcCartesianPoint( (0.0,1000.0,0.0) ) bar_line = ifc_file.createIfcPolyline([pnt1,pnt2,pnt3,pnt4]) bar_solid = ifc_file.createIfcSweptDiskSolidPolygonal(Directrix=bar_line, FilletRadius=250., Radius=6., 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(20): map = ifc_file.createIfcMappedItem() map.MappingSource = ifcRebarType.RepresentationMaps[0] T = ifc_file.createIfcCartesianTransformationOperator3D() LocalOrigin=ifc_file.createIfcCartesianPoint((i*100.,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=floor1) #============================================= filePath = r"C:\Users\sondr\OneDrive\Documents\IFC\myExports\selfMadeRebars.ifc" ifc_file.write(filePath)