/// <summary> /// Get the space name if any of the points are within the space /// </summary> /// <param name="PtsWCS">list of points</param> /// <param name="hitTotarance">Distance the point is outside the space but considered still usable to reference the space</param> /// <param name="SpaceBoundingBoxInfo"></param> /// <returns>Space name</returns> internal static string GetSpaceFromClosestPoints(IEnumerable <XbimPoint3D> PtsWCS, double hitTotarance, IEnumerable <SpaceInfo> SpaceBoundingBoxInfo) { //holder for space names, could be more then one so a list is used List <string> spaceNames = new List <string>(); foreach (SpaceInfo spGeoData in SpaceBoundingBoxInfo) { //get each space bounding box and To WCS Matrix XbimRect3D spBoundBox = spGeoData.Rectangle; XbimMatrix3D spWorldMatrix = spGeoData.Matrix; String spName = spGeoData.Name; //we need to transform the element max and min points back into the spaces Object Space so we can test on Bounding Box rectangle spWorldMatrix.Invert(); IEnumerable <XbimPoint3D> elBoxPtsOCS = PtsWCS.Select(pt => spWorldMatrix.Transform(pt)); //check if element space object points are contained fully within the space bounding box rectangle IEnumerable <double> hitPts = elBoxPtsOCS.Select(pt => DistanceFromSpace(pt, spBoundBox)).Where(d => d <= hitTotarance); if (hitPts.Any())//one or more point is contained in space and continue in case we have an element over several spaces { if (!spaceNames.Contains(spName)) { spaceNames.Add(spName); } } } if (spaceNames.Count > 0) { return(string.Join(", ", spaceNames)); } else { return(string.Empty); } }
public void TransformSolidRectangularProfileDef() { using (var m = new MemoryModel(new Xbim.Ifc4.EntityFactoryIfc4())) { using (var txn = m.BeginTransaction("Test")) { var profile = IfcModelBuilder.MakeRectangleHollowProfileDef(m, 20, 10, 1); var extrude = IfcModelBuilder.MakeExtrudedAreaSolid(m, profile, 40); var solid = _geomEngine.CreateSolid(extrude); var transform = new XbimMatrix3D(); //test first with identity var solid2 = (IXbimSolid)solid.Transform(transform); var s1Verts = solid.Vertices.ToList(); var s2Verts = solid2.Vertices.ToList(); for (int i = 0; i < s1Verts.Count; i++) { XbimVector3D v = s1Verts[i].VertexGeometry - s2Verts[i].VertexGeometry; Assert.IsTrue(v.Length < m.ModelFactors.Precision, "vertices not the same"); } transform.RotateAroundXAxis(Math.PI / 2); transform.RotateAroundYAxis(Math.PI / 4); transform.RotateAroundZAxis(Math.PI); transform.OffsetX += 100; transform.OffsetY += 200; transform.OffsetZ += 300; solid2 = (IXbimSolid)solid.Transform(transform); Assert.IsTrue(Math.Abs(solid.Volume - solid2.Volume) < 0.001, "Volume differs"); transform.Invert(); solid2 = (IXbimSolid)solid2.Transform(transform); s1Verts = solid.Vertices.ToList(); s2Verts = solid2.Vertices.ToList(); for (int i = 0; i < s1Verts.Count; i++) { XbimVector3D v = s1Verts[i].VertexGeometry - s2Verts[i].VertexGeometry; Assert.IsTrue(v.Length < m.ModelFactors.Precision, "vertices not the same"); } txn.Commit(); } } }
/// <summary> /// Calculate the ObjectPlacment for an IfcProduct from row data and the parent object /// </summary> /// <param name="row">COBieCoordinateRow holding the data</param> /// <param name="placementRelToIfcProduct">IfcProduct that the ObjectPlacment relates too, i.e. the parent of the ifcProduct ObjectPlacment we are calculating</param> /// <returns></returns> private IfcLocalPlacement CalcObjectPlacement(COBieCoordinateRow row, IfcProduct placementRelToIfcProduct) { XbimPoint3D locationPt; bool havePoint = GetPointFromRow(row, out locationPt); if (havePoint) { if ((placementRelToIfcProduct != null) && (placementRelToIfcProduct.ObjectPlacement is IfcLocalPlacement)) { //TEST, change the building position to see if the same point comes out in Excel sheet, it should be, and in test was. //((IfcAxis2Placement3D)((IfcLocalPlacement)placementRelToIfcProduct.ObjectPlacement).RelativePlacement).SetNewLocation(10.0, 10.0, 0.0); IfcLocalPlacement placementRelTo = (IfcLocalPlacement)placementRelToIfcProduct.ObjectPlacement; XbimMatrix3D matrix3D = ConvertMatrix3D(placementRelTo); //we want to take off the translations and rotations caused by IfcLocalPlacement of the parent objects as we will add these to the new IfcLocalPlacement for this floor matrix3D.Invert(); //so invert matrix to remove the translations to give the origin for the next IfcLocalPlacement locationPt = matrix3D.Transform(locationPt); //get the point with relation to the last IfcLocalPlacement i.e the parent element //Get the WCS matrix values double rotX, rotY, rotZ; if (!(double.TryParse(row.YawRotation, out rotX) && (double.NaN.CompareTo(rotX) != 0))) { rotX = 0.0; } if (!(double.TryParse(row.ElevationalRotation, out rotY) && (double.NaN.CompareTo(rotY) != 0))) { rotY = 0.0; } if (double.TryParse(row.ClockwiseRotation, out rotZ) && (double.NaN.CompareTo(rotZ) != 0)) { rotZ = rotZ * -1; //convert back from clockwise to anti clockwise } else { rotZ = 0.0; } //apply the WCS rotation from COBie Coordinates stored values XbimMatrix3D matrixNewRot3D = new XbimMatrix3D(); if (rotX != 0.0) { matrixNewRot3D.RotateAroundXAxis(TransformedBoundingBox.DegreesToRadians(rotX)); } if (rotY != 0.0) { matrixNewRot3D.RotateAroundYAxis(TransformedBoundingBox.DegreesToRadians(rotY)); } if (rotZ != 0.0) { matrixNewRot3D.RotateAroundZAxis(TransformedBoundingBox.DegreesToRadians(rotZ)); } //remove any displacement from the matrix which moved/rotated us to the object space matrix3D.OffsetX = 0.0F; matrix3D.OffsetY = 0.0F; matrix3D.OffsetZ = 0.0F; //remove the matrix that got use to the object space from the WCS location of this object XbimMatrix3D matrixRot3D = matrixNewRot3D * matrix3D; //get the rotation vectors to place in the new IfcAxis2Placement3D for the new IfcLocalPlacement for this object XbimVector3D ucsAxisX = matrixRot3D.Transform(new XbimVector3D(1, 0, 0)); XbimVector3D ucsAxisZ = matrixRot3D.Transform(new XbimVector3D(0, 0, 1)); ucsAxisX = ucsAxisX.Normalized(); ucsAxisZ = ucsAxisZ.Normalized(); //create the new IfcAxis2Placement3D IfcAxis2Placement3D relativePlacemant = Model.Instances.New <IfcAxis2Placement3D>(); relativePlacemant.SetNewDirectionOf_XZ(ucsAxisX.X, ucsAxisX.Y, ucsAxisX.Z, ucsAxisZ.X, ucsAxisZ.Y, ucsAxisZ.Z); relativePlacemant.SetNewLocation(locationPt.X, locationPt.Y, locationPt.Z); //Set up IfcLocalPlacement IfcLocalPlacement objectPlacement = Model.Instances.New <IfcLocalPlacement>(); objectPlacement.PlacementRelTo = placementRelTo; objectPlacement.RelativePlacement = relativePlacemant; return(objectPlacement); } } return(null); }
/// <summary> /// Add a Bounding Box extrusion onto the ifcProduct /// </summary> /// <param name="row">COBieCoordinateRow holding the data for one corner</param> /// <param name="rowNext">COBieCoordinateRow holding the data for the other corner</param> /// <param name="placementRelToIfcProduct">Product which is parent of ifcProduct passed product to add extrusion onto</param> /// <param name="ifcProduct">IfcProduct to add the extrusion onto</param> private void AddExtrudedRectangle(COBieCoordinateRow row, COBieCoordinateRow rowNext, IfcProduct ifcProduct, IfcProduct placementRelToIfcProduct) { if (ifcProduct != null) { COBieCoordinateRow lowerLeftRow, upperRightRow; if (row.Category.ToLower() == "box-lowerleft") { lowerLeftRow = row; upperRightRow = rowNext; } else { lowerLeftRow = rowNext; upperRightRow = row; } IfcLocalPlacement objectPlacement = CalcObjectPlacement(lowerLeftRow, placementRelToIfcProduct); if (objectPlacement != null) { //set the object placement for the space ifcProduct.ObjectPlacement = objectPlacement; //get matrix to the space placement XbimMatrix3D matrix3D = ConvertMatrix3D(objectPlacement); //invert matrix so we can convert row points back to the object space matrix3D.Invert(); //lets get the points from the two rows XbimPoint3D lowpt, highpt; if ((GetPointFromRow(upperRightRow, out highpt)) && (GetPointFromRow(lowerLeftRow, out lowpt)) ) { //transform the points back to object space lowpt = matrix3D.Transform(lowpt); highpt = matrix3D.Transform(highpt); //in object space so we can use Rect3D as this will be aligned with coordinates systems X and Y XbimRect3D bBox = new XbimRect3D(); bBox.Location = lowpt; bBox.Union(highpt); if ((double.NaN.CompareTo(bBox.SizeX) != 0) && (double.NaN.CompareTo(bBox.SizeY) != 0)) { XbimPoint3D ctrPt = new XbimPoint3D(bBox.X + (bBox.SizeX / 2.0), bBox.Y + (bBox.SizeY / 2.0), bBox.Z + (bBox.SizeZ / 2.0)); //Create IfcRectangleProfileDef IfcCartesianPoint IfcCartesianPointCtr = Model.Instances.New <IfcCartesianPoint>(cp => { cp.X = ctrPt.X; cp.Y = ctrPt.Y; cp.Z = 0.0; }); //centre point of 2D box IfcDirection IfcDirectionXDir = Model.Instances.New <IfcDirection>(d => { d.X = 1.0; d.Y = 0; d.Z = 0.0; }); //default to X direction IfcAxis2Placement2D ifcAxis2Placement2DCtr = Model.Instances.New <IfcAxis2Placement2D>(a2p => { a2p.Location = IfcCartesianPointCtr; a2p.RefDirection = IfcDirectionXDir; }); IfcRectangleProfileDef ifcRectangleProfileDef = Model.Instances.New <IfcRectangleProfileDef>(rpd => { rpd.ProfileType = IfcProfileTypeEnum.AREA; rpd.ProfileName = row.RowName; rpd.Position = ifcAxis2Placement2DCtr; rpd.XDim = bBox.SizeX; rpd.YDim = bBox.SizeY; }); //Create IfcExtrudedAreaSolid IfcDirection IfcDirectionAxis = Model.Instances.New <IfcDirection>(d => { d.X = 0.0; d.Y = 0; d.Z = 1.0; }); //default to Z direction IfcDirection IfcDirectionRefDir = Model.Instances.New <IfcDirection>(d => { d.X = 1.0; d.Y = 0; d.Z = 0.0; }); //default to X direction IfcCartesianPoint IfcCartesianPointPosition = Model.Instances.New <IfcCartesianPoint>(cp => { cp.X = 0.0; cp.Y = 0.0; cp.Z = 0.0; }); //centre point of 2D box IfcAxis2Placement3D ifcAxis2Placement3DPosition = Model.Instances.New <IfcAxis2Placement3D>(a2p3D => { a2p3D.Location = IfcCartesianPointPosition; a2p3D.Axis = IfcDirectionAxis; a2p3D.RefDirection = IfcDirectionRefDir; }); IfcDirection IfcDirectionExtDir = Model.Instances.New <IfcDirection>(d => { d.X = 0.0; d.Y = 0; d.Z = 1.0; }); //default to Z direction IfcExtrudedAreaSolid ifcExtrudedAreaSolid = Model.Instances.New <IfcExtrudedAreaSolid>(eas => { eas.SweptArea = ifcRectangleProfileDef; eas.Position = ifcAxis2Placement3DPosition; eas.ExtrudedDirection = IfcDirectionExtDir; eas.Depth = bBox.SizeZ; }); var project = Model.FederatedInstances.OfType <IfcProject>().FirstOrDefault(); //Create IfcShapeRepresentation IfcShapeRepresentation ifcShapeRepresentation = Model.Instances.New <IfcShapeRepresentation>(sr => { sr.ContextOfItems = project.ModelContext; sr.RepresentationIdentifier = "Body"; sr.RepresentationType = "SweptSolid"; }); ifcShapeRepresentation.Items.Add(ifcExtrudedAreaSolid); //create IfcProductDefinitionShape IfcProductDefinitionShape ifcProductDefinitionShape = Model.Instances.New <IfcProductDefinitionShape>(pds => { pds.Name = row.Name; pds.Description = row.SheetName; }); ifcProductDefinitionShape.Representations.Add(ifcShapeRepresentation); //Link to the IfcProduct ifcProduct.Representation = ifcProductDefinitionShape; } else { #if DEBUG Console.WriteLine("Failed to calculate box size for {0}", row.Name); #endif } } } else { #if DEBUG Console.WriteLine("Failed to add Object placement for {0}", row.Name); #endif } } }