public static Surface GetSurface(this Face face) { switch (face) { case PlanarFace planarFace: return(Plane.CreateByOriginAndBasis(planarFace.Origin, planarFace.XVector, planarFace.YVector)); case ConicalFace conicalFace: { var basisX = conicalFace.get_Radius(0).Normalize(); var basisY = conicalFace.get_Radius(1).Normalize(); var basisZ = conicalFace.Axis.Normalize(); return(ConicalSurface.Create(new Frame(conicalFace.Origin, basisX, basisY, basisZ), conicalFace.HalfAngle)); } case CylindricalFace cylindricalFace: { double radius = cylindricalFace.get_Radius(0).GetLength(); var basisX = cylindricalFace.get_Radius(0).Normalize(); var basisY = cylindricalFace.get_Radius(1).Normalize(); var basisZ = cylindricalFace.Axis.Normalize(); return(CylindricalSurface.Create(new Frame(cylindricalFace.Origin, basisX, basisY, basisZ), radius)); } case RevolvedFace revolvedFace: { var ECStoWCS = new Transform(Transform.Identity) { Origin = revolvedFace.Origin, BasisX = revolvedFace.get_Radius(0).Normalize(), BasisY = revolvedFace.get_Radius(1).Normalize(), BasisZ = revolvedFace.Axis.Normalize() }; var profileInWCS = revolvedFace.Curve.CreateTransformed(ECStoWCS); return(RevolvedSurface.Create(new Frame(ECStoWCS.Origin, ECStoWCS.BasisX, ECStoWCS.BasisY, ECStoWCS.BasisZ), profileInWCS)); } case RuledFace ruledFace: { var profileCurve0 = ruledFace.get_Curve(0); var profileCurve1 = ruledFace.get_Curve(1); return(RuledSurface.Create(profileCurve0, profileCurve1)); } } return(null); }
/// <summary> /// Create a cylinder to subtract from the cube. /// </summary> public BRepBuilder CreateBrepVoid() { // Naming convention for faces and edges: we // assume that x is to the left and pointing down, // y is horizontal and pointing to the right, // z is up. BRepBuilder b = new BRepBuilder(BRepType.Solid); // The surfaces of the four faces. Frame basis = new Frame(new XYZ(50, 0, 0), new XYZ(0, 1, 0), new XYZ(-1, 0, 0), new XYZ(0, 0, 1)); CylindricalSurface cylSurf = CylindricalSurface.Create(basis, 40); Plane top1 = Plane.CreateByNormalAndOrigin(new XYZ(0, 0, 1), new XYZ(0, 0, 100)); // normal points outside the cylinder Plane bottom1 = Plane.CreateByNormalAndOrigin(new XYZ(0, 0, 1), new XYZ(0, 0, 0)); // normal points inside the cylinder // Add the four faces BRepBuilderGeometryId frontCylFaceId = b.AddFace(BRepBuilderSurfaceGeometry.Create(cylSurf, null), false); BRepBuilderGeometryId backCylFaceId = b.AddFace(BRepBuilderSurfaceGeometry.Create(cylSurf, null), false); BRepBuilderGeometryId topFaceId = b.AddFace(BRepBuilderSurfaceGeometry.Create(top1, null), false); BRepBuilderGeometryId bottomFaceId = b.AddFace(BRepBuilderSurfaceGeometry.Create(bottom1, null), true); // Geometry for the four semi-circular edges and two vertical linear edges BRepBuilderEdgeGeometry frontEdgeBottom = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(10, 0, 0), new XYZ(90, 0, 0), new XYZ(50, 40, 0))); BRepBuilderEdgeGeometry backEdgeBottom = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(90, 0, 0), new XYZ(10, 0, 0), new XYZ(50, -40, 0))); BRepBuilderEdgeGeometry frontEdgeTop = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(10, 0, 100), new XYZ(90, 0, 100), new XYZ(50, 40, 100))); BRepBuilderEdgeGeometry backEdgeTop = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(10, 0, 100), new XYZ(90, 0, 100), new XYZ(50, -40, 100))); BRepBuilderEdgeGeometry linearEdgeFront = BRepBuilderEdgeGeometry.Create(new XYZ(90, 0, 0), new XYZ(90, 0, 100)); BRepBuilderEdgeGeometry linearEdgeBack = BRepBuilderEdgeGeometry.Create(new XYZ(10, 0, 0), new XYZ(10, 0, 100)); // Add the six edges BRepBuilderGeometryId frontEdgeBottomId = b.AddEdge(frontEdgeBottom); BRepBuilderGeometryId frontEdgeTopId = b.AddEdge(frontEdgeTop); BRepBuilderGeometryId linearEdgeFrontId = b.AddEdge(linearEdgeFront); BRepBuilderGeometryId linearEdgeBackId = b.AddEdge(linearEdgeBack); BRepBuilderGeometryId backEdgeBottomId = b.AddEdge(backEdgeBottom); BRepBuilderGeometryId backEdgeTopId = b.AddEdge(backEdgeTop); // Loops of the four faces BRepBuilderGeometryId loopId_Top = b.AddLoop(topFaceId); BRepBuilderGeometryId loopId_Bottom = b.AddLoop(bottomFaceId); BRepBuilderGeometryId loopId_Front = b.AddLoop(frontCylFaceId); BRepBuilderGeometryId loopId_Back = b.AddLoop(backCylFaceId); // Add coedges for the loop of the front face b.AddCoEdge(loopId_Front, linearEdgeBackId, false); b.AddCoEdge(loopId_Front, frontEdgeTopId, false); b.AddCoEdge(loopId_Front, linearEdgeFrontId, true); b.AddCoEdge(loopId_Front, frontEdgeBottomId, true); b.FinishLoop(loopId_Front); b.FinishFace(frontCylFaceId); // Add coedges for the loop of the back face b.AddCoEdge(loopId_Back, linearEdgeBackId, true); b.AddCoEdge(loopId_Back, backEdgeBottomId, true); b.AddCoEdge(loopId_Back, linearEdgeFrontId, false); b.AddCoEdge(loopId_Back, backEdgeTopId, true); b.FinishLoop(loopId_Back); b.FinishFace(backCylFaceId); // Add coedges for the loop of the top face b.AddCoEdge(loopId_Top, backEdgeTopId, false); b.AddCoEdge(loopId_Top, frontEdgeTopId, true); b.FinishLoop(loopId_Top); b.FinishFace(topFaceId); // Add coedges for the loop of the bottom face b.AddCoEdge(loopId_Bottom, frontEdgeBottomId, false); b.AddCoEdge(loopId_Bottom, backEdgeBottomId, false); b.FinishLoop(loopId_Bottom); b.FinishFace(bottomFaceId); b.Finish(); return(b); }
/// <summary> /// 创建圆柱 /// </summary> public static void CreateCylinder(Document doc, Material mat) { // Naming convention for faces and edges: we assume that x is to the left and pointing down, y is horizontal and pointing to the right, z is up BRepBuilder brepBuilder = new BRepBuilder(BRepType.Solid); // The surfaces of the four faces. Frame basis = new Frame(new XYZ(5, -10, 0), new XYZ(0, 1, 0), new XYZ(-1, 0, 0), new XYZ(0, 0, 1)); // Note that we do not have to create two identical surfaces here. The same surface can be used for multiple faces, // since BRepBuilderSurfaceGeometry::Create() copies the input surface. // Thus, potentially we could have only one surface here, // but we must create at least two faces below to account for periodicity. CylindricalSurface frontCylSurf = CylindricalSurface.Create(basis, 5); CylindricalSurface backCylSurf = CylindricalSurface.Create(basis, 5); Plane top = Plane.CreateByNormalAndOrigin(new XYZ(0, 0, 1), new XYZ(0, 0, 100)); // normal points outside the cylinder Plane bottom = Plane.CreateByNormalAndOrigin(new XYZ(0, 0, 1), new XYZ(0, 0, 0)); // normal points inside the cylinder // Note that the alternating of "inside/outside" matches the alternating of "true/false" in the next block that defines faces. // There must be a correspondence to ensure that all faces are correctly oriented to point out of the solid. // Add the four faces BRepBuilderGeometryId frontCylFaceId = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(frontCylSurf, null), false); BRepBuilderGeometryId backCylFaceId = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(backCylSurf, null), false); BRepBuilderGeometryId topFaceId = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(top, null), false); BRepBuilderGeometryId bottomFaceId = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(bottom, null), true); brepBuilder.SetFaceMaterialId(frontCylFaceId, mat.Id); brepBuilder.SetFaceMaterialId(backCylFaceId, mat.Id); // Geometry for the four semi-circular edges and two vertical linear edges BRepBuilderEdgeGeometry frontEdgeBottom = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(0, -10, 0), new XYZ(10, -10, 0), new XYZ(5, -5, 0))); BRepBuilderEdgeGeometry backEdgeBottom = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(10, -10, 0), new XYZ(0, -10, 0), new XYZ(5, -15, 0))); BRepBuilderEdgeGeometry frontEdgeTop = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(0, -10, 100), new XYZ(10, -10, 100), new XYZ(5, -5, 100))); BRepBuilderEdgeGeometry backEdgeTop = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(0, -10, 100), new XYZ(10, -10, 100), new XYZ(5, -15, 100))); BRepBuilderEdgeGeometry linearEdgeFront = BRepBuilderEdgeGeometry.Create(new XYZ(10, -10, 0), new XYZ(10, -10, 100)); BRepBuilderEdgeGeometry linearEdgeBack = BRepBuilderEdgeGeometry.Create(new XYZ(0, -10, 0), new XYZ(0, -10, 100)); // Add the six edges BRepBuilderGeometryId frontEdgeBottomId = brepBuilder.AddEdge(frontEdgeBottom); BRepBuilderGeometryId frontEdgeTopId = brepBuilder.AddEdge(frontEdgeTop); BRepBuilderGeometryId linearEdgeFrontId = brepBuilder.AddEdge(linearEdgeFront); BRepBuilderGeometryId linearEdgeBackId = brepBuilder.AddEdge(linearEdgeBack); BRepBuilderGeometryId backEdgeBottomId = brepBuilder.AddEdge(backEdgeBottom); BRepBuilderGeometryId backEdgeTopId = brepBuilder.AddEdge(backEdgeTop); // Loops of the four faces BRepBuilderGeometryId loopId_Top = brepBuilder.AddLoop(topFaceId); BRepBuilderGeometryId loopId_Bottom = brepBuilder.AddLoop(bottomFaceId); BRepBuilderGeometryId loopId_Front = brepBuilder.AddLoop(frontCylFaceId); BRepBuilderGeometryId loopId_Back = brepBuilder.AddLoop(backCylFaceId); // Add coedges for the loop of the front face brepBuilder.AddCoEdge(loopId_Front, linearEdgeBackId, false); brepBuilder.AddCoEdge(loopId_Front, frontEdgeTopId, false); brepBuilder.AddCoEdge(loopId_Front, linearEdgeFrontId, true); brepBuilder.AddCoEdge(loopId_Front, frontEdgeBottomId, true); brepBuilder.FinishLoop(loopId_Front); brepBuilder.FinishFace(frontCylFaceId); // Add coedges for the loop of the back face brepBuilder.AddCoEdge(loopId_Back, linearEdgeBackId, true); brepBuilder.AddCoEdge(loopId_Back, backEdgeBottomId, true); brepBuilder.AddCoEdge(loopId_Back, linearEdgeFrontId, false); brepBuilder.AddCoEdge(loopId_Back, backEdgeTopId, true); brepBuilder.FinishLoop(loopId_Back); brepBuilder.FinishFace(backCylFaceId); // Add coedges for the loop of the top face brepBuilder.AddCoEdge(loopId_Top, backEdgeTopId, false); brepBuilder.AddCoEdge(loopId_Top, frontEdgeTopId, true); brepBuilder.FinishLoop(loopId_Top); brepBuilder.FinishFace(topFaceId); // Add coedges for the loop of the bottom face brepBuilder.AddCoEdge(loopId_Bottom, frontEdgeBottomId, false); brepBuilder.AddCoEdge(loopId_Bottom, backEdgeBottomId, false); brepBuilder.FinishLoop(loopId_Bottom); brepBuilder.FinishFace(bottomFaceId); brepBuilder.Finish(); createDirectShapeElementFromBrepBuilderObject(brepBuilder, "Full cylinder", doc); }