public MeshConverter(PointConverter ptConv) : base( (rm) => { ppg.Mesh pm = new ppg.Mesh(); pm.Vertices.AddRange(rm.Vertices.Select(pt => ptConv.ToPipe <rg.XYZ, ppg.Vec>(pt))); for (int i = 0; i < rm.NumTriangles; i++) { rg.MeshTriangle tr = rm.get_Triangle(i); pm.Faces.Add(new ulong[] { tr.get_Index(0), tr.get_Index(1), tr.get_Index(2) }); } return(pm); }, (ppm) => { //making sure to triangulate so that revit doesn't fail ppm.Triangulate(); rg.TessellatedShapeBuilder builder = new rg.TessellatedShapeBuilder(); builder.OpenConnectedFaceSet(true); builder.Target = rg.TessellatedShapeBuilderTarget.Mesh; builder.Fallback = rg.TessellatedShapeBuilderFallback.Salvage; List <rg.XYZ> allVerts = ppm.Vertices.Select((pt) => ptConv.FromPipe <rg.XYZ, ppg.Vec>(pt)).ToList(); foreach (var face in ppm.Faces) { List <rg.XYZ> faceVerts = new List <rg.XYZ>(); var faceReversed = face.Reverse().ToList(); foreach (ulong vertIndex in faceReversed) { faceVerts.Add(allVerts[(int)vertIndex]); } rg.TessellatedFace tface = new rg.TessellatedFace(faceVerts, rg.ElementId.InvalidElementId); //if (!builder.DoesFaceHaveEnoughLoopsAndVertices(tface)) { continue; } builder.AddFace(tface); } builder.CloseConnectedFaceSet(); builder.Build(); rg.TessellatedShapeBuilderResult result = builder.GetBuildResult(); List <rg.GeometryObject> geoms = result.GetGeometricalObjects().ToList(); return((rg.Mesh)geoms.FirstOrDefault()); } ) { }
public CurveConverter(PointConverter ptConv, PipeConverter <rg.Plane, ppg.Plane> planeConv) { //converting lines var lineConv = AddConverter(new PipeConverter <rg.Line, ppc.Line>( (rl) => { return(new ppc.Line(ptConv.ToPipe <rg.XYZ, ppg.Vec>(rl.GetEndPoint(0)), ptConv.ToPipe <rg.XYZ, ppg.Vec>(rl.GetEndPoint(1)))); }, (ppl) => { return(rg.Line.CreateBound(ptConv.FromPipe <rg.XYZ, ppg.Vec>(ppl.StartPoint), ptConv.FromPipe <rg.XYZ, ppg.Vec>(ppl.EndPoint))); } )); //converting arcs var arcConv = AddConverter(new PipeConverter <rg.Arc, ppc.Arc>( (rarc) => { ppg.Vec startPt = ptConv.ToPipe <rg.XYZ, ppg.Vec>(rarc.GetEndPoint(0)); ppg.Vec endPt = ptConv.ToPipe <rg.XYZ, ppg.Vec>(rarc.GetEndPoint(1)); ppg.Vec midPt = ptConv.ToPipe <rg.XYZ, ppg.Vec>(rarc.Evaluate(0.5, true)); return(new ppc.Arc(startPt, endPt, midPt)); }, (parc) => { return(rg.Arc.Create(planeConv.FromPipe <rg.Plane, ppg.Plane>(parc.Plane), parc.Radius, parc.StartAngle, parc.EndAngle)); } )); //converting nurbs curves var nurbsConv = AddConverter(new PipeConverter <rg.NurbSpline, ppc.NurbsCurve>( (rnc) => { return(new ppc.NurbsCurve(rnc.CtrlPoints.Select( (pt) => ptConv.ToPipe <rg.XYZ, ppg.Vec>(pt)).ToList(), rnc.Degree, rnc.Weights.Cast <double>().ToList(), rnc.Knots.Cast <double>().ToList(), rnc.isClosed)); }, null )); var nurbsConv2 = AddConverter(new PipeConverter <rg.Curve, ppc.NurbsCurve>( null, (pnc) => { if (pnc.IsClosed) { //revit doesn't support closed nurbs curves Utils.RevitPipeUtil.ShowMessage("Conversion failed", "Closed NurbSplines are not supported", "Revit doesn't support closed Nurbs curves. Revit's website says you can instead close one" + "Nurbs curve with another."); return(null); } var ptList = pnc.ControlPoints.Select((pt) => ptConv.FromPipe <rg.XYZ, ppg.Vec>(pt)).ToList(); return(rg.NurbSpline.CreateCurve(ptList, pnc.Weights.ToList())); } )); }
public RevitPipeConverter() { var ptConv = new PointConverter(); AddConverter(ptConv); var planeConv = AddConverter(new PipeConverter <rg.Plane, ppg.Plane>( (rpl) => { return(new ppg.Plane(ptConv.ToPipe <rg.XYZ, ppg.Vec>(rpl.Origin), ptConv.ToPipe <rg.XYZ, ppg.Vec>(rpl.XVec), ptConv.ToPipe <rg.XYZ, ppg.Vec>(rpl.YVec), ptConv.ToPipe <rg.XYZ, ppg.Vec>(rpl.Normal))); }, (ppl) => { return(rg.Plane.CreateByOriginAndBasis(ptConv.FromPipe <rg.XYZ, ppg.Vec>(ppl.Origin), ptConv.FromPipe <rg.XYZ, ppg.Vec>(ppl.X), ptConv.FromPipe <rg.XYZ, ppg.Vec>(ppl.Y))); } )); var geomConv = new GeometryConverter(ptConv, planeConv); AddConverter(geomConv); var polylineListConv = AddConverter(new PipeConverter <rg.Line[], ppc.Polyline>( (rlg) => { List <rg.XYZ> pts = rlg.Select((ln) => ln.GetEndPoint(0)).ToList(); pts.Add(rlg.LastOrDefault().GetEndPoint(1)); return(new ppc.Polyline(pts.Select((pt) => ptConv.ToPipe <rg.XYZ, ppg.Vec>(pt)).ToList())); }, (ppl) => { List <ppc.Line> lines = ppl.ExplodedLines(); return(lines.Select((ln) => (rg.Line)geomConv.FromPipe <rg.GeometryObject, IPipeMemberType>(ln)).ToArray()); } )); //extrusions var extrConv = AddConverter(new PipeConverter <rg.Extrusion, pps.Extrusion>( (rext) => { rg.XYZ norm = rext.Sketch.SketchPlane.GetPlane().Normal.Normalize(); var startNorm = norm.Multiply(rext.StartOffset); var endNorm = norm.Normalize().Multiply(rext.EndOffset); var depthVec = norm.Normalize().Multiply(rext.EndOffset - rext.StartOffset); List <ppc.Curve> curs = new List <ppc.Curve>(); foreach (rg.Curve cur in rext.Sketch.Profile) { curs.Add(geomConv.CurveConverter.ToPipe <rg.Curve, ppc.Curve>(cur.CreateTransformed( rg.Transform.CreateTranslation(startNorm)))); } return(new pps.Extrusion(new ppc.PolyCurve(curs), ptConv.ToPipe <rg.XYZ, ppg.Vec>(depthVec), rext.EndOffset - rext.StartOffset)); }, (pe) => { double tolerance = 1e-3; rg.CurveArrArray profile = new rg.CurveArrArray(); rg.CurveArray curs = new rg.CurveArray(); List <ppc.Curve> pCurs = pe.ProfileCurve.FlattenedCurveList(); rg.Plane plane = null; pCurs.ForEach((cur) => { rg.Curve revitCur = geomConv.CurveConverter.FromPipe <rg.Curve, ppc.Curve>(cur); curs.Append(revitCur); rg.Plane newPl = Utils.SketchPlaneUtil.GetPlaneForCurve(revitCur); if (plane == null) { plane = newPl; } else if (Math.Abs(plane.Normal.Normalize().DotProduct(newPl.Normal.Normalize()) - 1) > tolerance) { //the two planes are not aligned so throw exception throw new InvalidOperationException("Cannot create a Revit Extrusion because the profile " + "curves are not in the same plane"); } }); profile.Append(curs); rg.XYZ dir = ptConv.FromPipe <rg.XYZ, ppg.Vec>(pe.Direction); if (Math.Abs(plane.Normal.Normalize().DotProduct(dir.Normalize()) - 1) > tolerance) { throw new NotImplementedException("Extrusions with direction not perpendicular to curve" + "cannot be imported into revit, try converting it to a mesh before sending through the pipe"); } return(PipeForRevit.ActiveDocument.FamilyCreate.NewExtrusion(false, profile, rg.SketchPlane.Create(PipeForRevit.ActiveDocument, plane), pe.Height)); } )); }