/// <summary> /// Create line element /// </summary> /// <param name="app">revit application</param> /// <param name="ptA">start point</param> /// <param name="ptB">end point</param> /// <returns></returns> public static ModelCurve MakeLine(UIApplication app, Autodesk.Revit.DB.XYZ ptA, Autodesk.Revit.DB.XYZ ptB) { Document doc = app.ActiveUIDocument.Document; // Create plane by the points Line line = Line.CreateBound(ptA, ptB); Autodesk.Revit.DB.XYZ norm = ptA.CrossProduct(ptB); if (norm.GetLength() == 0) { norm = Autodesk.Revit.DB.XYZ.BasisZ; } Plane plane = app.Application.Create.NewPlane(norm, ptB); SketchPlane skplane = SketchPlane.Create(doc, plane); // Create line here ModelCurve modelcurve = doc.FamilyCreate.NewModelCurve(line, skplane); return(modelcurve); }
/// <summary> /// constructor /// </summary> /// <param name="element">The host object, must be family instance</param> public GeometrySupport(FamilyInstance element) { // get the geometry element of the selected element Autodesk.Revit.DB.GeometryElement geoElement = element.get_Geometry(new Options()); IEnumerator <GeometryObject> Objects = geoElement.GetEnumerator(); //if (null == geoElement || 0 == geoElement.Objects.Size) if (null == geoElement || !Objects.MoveNext()) { throw new Exception("Can't get the geometry of selected element."); } SweptProfile swProfile = element.GetSweptProfile(); if (swProfile == null || !(swProfile.GetDrivingCurve() is Line)) { throw new Exception("The selected element driving curve is not a line."); } // get the driving path and vector of the beam or column Line line = swProfile.GetDrivingCurve() as Line; if (null != line) { m_drivingLine = line; // driving path m_drivingVector = GeomUtil.SubXYZ(line.GetEndPoint(1), line.GetEndPoint(0)); m_drivingLength = m_drivingVector.GetLength(); } //get the geometry object //foreach (GeometryObject geoObject in geoElement.Objects) Objects.Reset(); while (Objects.MoveNext()) { GeometryObject geoObject = Objects.Current; //get the geometry instance which contains the geometry information GeoInstance instance = geoObject as GeoInstance; if (null != instance) { //foreach (GeometryObject o in instance.SymbolGeometry.Objects) IEnumerator <GeometryObject> Objects1 = instance.SymbolGeometry.GetEnumerator(); while (Objects1.MoveNext()) { GeometryObject o = Objects1.Current; // get the solid of beam of column Solid solid = o as Solid; // do some checks. if (null == solid) { continue; } if (0 == solid.Faces.Size || 0 == solid.Edges.Size) { continue; } m_solid = solid; //get the transform value of instance m_transform = instance.Transform; // Get the swept profile curves information if (!GetSweptProfile(solid)) { throw new Exception("Can't get the swept profile curves."); } break; } } } // do some checks about profile curves information if (null == m_edges) { throw new Exception("Can't get the geometry edge information."); } if (4 != m_points.Count) { throw new Exception("The sample only works for rectangle beam or column."); } }
/// <summary> /// Detect the obstructions of pipe and resolve them. /// </summary> /// <param name="pipe">Pipe to resolve</param> private void Resolve(Pipe pipe) { // Get the centerline of pipe. Line pipeLine = (pipe.Location as LocationCurve).Curve as Line; // Calculate the intersection references with pipe's centerline. List <ReferenceWithContext> obstructionRefArr = m_detector.Obstructions(pipeLine); // Filter out the references, just allow Pipe, Beam, and Duct. Filter(pipe, obstructionRefArr); if (obstructionRefArr.Count == 0) { // There are no intersection found. return; } // Calculate the direction of pipe's centerline. Autodesk.Revit.DB.XYZ dir = pipeLine.GetEndPoint(1) - pipeLine.GetEndPoint(0); // Build the sections from the intersection references. List <Section> sections = Section.BuildSections(obstructionRefArr, dir.Normalize()); // Merge the neighbor sections if the distance of them is too close. for (int i = sections.Count - 2; i >= 0; i--) { Autodesk.Revit.DB.XYZ detal = sections[i].End - sections[i + 1].Start; if (detal.GetLength() < pipe.Diameter * 3) { sections[i].Refs.AddRange(sections[i + 1].Refs); sections.RemoveAt(i + 1); } } // Resolve the obstructions one by one. foreach (Section sec in sections) { Resolve(pipe, sec); } // Connect the neighbor sections with pipe and elbow fittings. // for (int i = 1; i < sections.Count; i++) { // Get the end point from the previous section. Autodesk.Revit.DB.XYZ start = sections[i - 1].End; // Get the start point from the current section. Autodesk.Revit.DB.XYZ end = sections[i].Start; // Create a pipe between two neighbor section. Pipe tmpPipe = m_rvtDoc.Create.NewPipe(start, end, pipe.PipeType); // Copy pipe's parameters values to tmpPipe. CopyParameters(pipe, tmpPipe); // Create elbow fitting to connect previous section with tmpPipe. Connector conn1 = FindConnector(sections[i - 1].Pipes[2], start); Connector conn2 = FindConnector(tmpPipe, start); FamilyInstance fi = m_rvtDoc.Create.NewElbowFitting(conn1, conn2); // Create elbow fitting to connect current section with tmpPipe. Connector conn3 = FindConnector(sections[i].Pipes[0], end); Connector conn4 = FindConnector(tmpPipe, end); FamilyInstance f2 = m_rvtDoc.Create.NewElbowFitting(conn3, conn4); } // Find two connectors which pipe's two ends connector connected to. Connector startConn = FindConnectedTo(pipe, pipeLine.GetEndPoint(0)); Connector endConn = FindConnectedTo(pipe, pipeLine.GetEndPoint(1)); Pipe startPipe = null; if (null != startConn) { // Create a pipe between pipe's start connector and pipe's start section. startPipe = m_rvtDoc.Create.NewPipe(sections[0].Start, startConn, pipe.PipeType); } else { // Create a pipe between pipe's start point and pipe's start section. startPipe = m_rvtDoc.Create.NewPipe(sections[0].Start, pipeLine.GetEndPoint(0), pipe.PipeType); } // Copy parameters from pipe to startPipe. CopyParameters(pipe, startPipe); // Connect the startPipe and first section with elbow fitting. Connector connStart1 = FindConnector(startPipe, sections[0].Start); Connector connStart2 = FindConnector(sections[0].Pipes[0], sections[0].Start); FamilyInstance fii = m_rvtDoc.Create.NewElbowFitting(connStart1, connStart2); Pipe endPipe = null; int count = sections.Count; if (null != endConn) { // Create a pipe between pipe's end connector and pipe's end section. endPipe = m_rvtDoc.Create.NewPipe(sections[count - 1].End, endConn, pipe.PipeType); } else { // Create a pipe between pipe's end point and pipe's end section. endPipe = m_rvtDoc.Create.NewPipe(sections[count - 1].End, pipeLine.GetEndPoint(1), pipe.PipeType); } // Copy parameters from pipe to endPipe. CopyParameters(pipe, endPipe); // Connect the endPipe and last section with elbow fitting. Connector connEnd1 = FindConnector(endPipe, sections[count - 1].End); Connector connEnd2 = FindConnector(sections[count - 1].Pipes[2], sections[count - 1].End); FamilyInstance fiii = m_rvtDoc.Create.NewElbowFitting(connEnd1, connEnd2); // Delete the pipe after resolved. m_rvtDoc.Delete(pipe.Id); }
/// <summary> /// constructor /// </summary> /// <param name="element">The host object, must be family instance</param> public GeometrySupport(FamilyInstance element) { // get the geometry element of the selected element Autodesk.Revit.DB.GeometryElement geoElement = element.get_Geometry(new Options()); if (null == geoElement || 0 == geoElement.Objects.Size) { throw new Exception("Can't get the geometry of selected element."); } AnalyticalModel aModel = element.GetAnalyticalModel(); if (aModel == null) { throw new Exception("The selected FamilyInstance don't have AnalyticalModel."); } AnalyticalModelSweptProfile swProfile = aModel.GetSweptProfile(); if (swProfile == null || !(swProfile.GetDrivingCurve() is Line)) { throw new Exception("The selected element driving curve is not a line."); } // get the driving path and vector of the beam or column Line line = swProfile.GetDrivingCurve() as Line; if (null != line) { m_drivingLine = line; // driving path m_drivingVector = GeomUtil.SubXYZ(line.get_EndPoint(1), line.get_EndPoint(0)); m_drivingLength = m_drivingVector.GetLength(); } //get the geometry object foreach (GeometryObject geoObject in geoElement.Objects) { //get the geometry instance which contains the geometry information GeoInstance instance = geoObject as GeoInstance; if (null != instance) { foreach (GeometryObject o in instance.SymbolGeometry.Objects) { // get the solid of beam of column Solid solid = o as Solid; // do some checks. if (null == solid) { continue; } if (0 == solid.Faces.Size || 0 == solid.Edges.Size) { continue; } m_solid = solid; //get the transform value of instance m_transform = instance.Transform; // Get the swept profile curves information if (!GetSweptProfile(solid)) { throw new Exception("Can't get the swept profile curves."); } break; } } } // do some checks about profile curves information if (null == m_edges) { throw new Exception("Can't get the geometry edge information."); } if (4 != m_points.Count) { throw new Exception("The sample only works for rectangle beam or column."); } }