/// <summary> /// Create a geometry object(s) described by stored face sets, if possible. /// Usually a single-element IList conatining either Solid or Mesh is returned. /// A two-elemant IList containing a Solid as the 1st element and a Mesh as /// the 2nd is returned if while building multiple face sets, a fallback /// was used for some but not all sets. /// </summary> /// <returns>The IList created, or null. The IList can contain a Solid and/or a Mesh. /// If Solid is present, it always the 1st element.</returns> private IList <GeometryObject> CreateGeometryObjects(string guid, out bool hasInvalidData, out TessellatedShapeBuilderOutcome outcome) { try { TessellatedShapeBuilder.CloseConnectedFaceSet(); // The OwnerInfo is currently unused; the value doesn't really matter. TessellatedShapeBuilder.LogString = IFCImportFile.TheFileName; TessellatedShapeBuilder.LogInteger = IFCImportFile.TheBrepCounter; TessellatedShapeBuilder.OwnerInfo = guid != null ? guid : "Temporary Element"; TessellatedShapeBuilder.Target = TargetGeometry; TessellatedShapeBuilder.Fallback = FallbackGeometry; TessellatedShapeBuilder.GraphicsStyleId = GraphicsStyleId; TessellatedShapeBuilder.Build(); TessellatedShapeBuilderResult result = TessellatedShapeBuilder.GetBuildResult(); // It is important that we clear the TSB after we build above, otherwise we will "collect" geometries // in the DirectShape and create huge files with redundant data. ClearTessellatedShapeBuilder(); hasInvalidData = result.HasInvalidData; outcome = result.Outcome; return(result.GetGeometricalObjects()); } catch (Exception ex) { Importer.TheLog.LogError(CreatorId(), ex.Message, false); ClearTessellatedShapeBuilder(); hasInvalidData = true; outcome = TessellatedShapeBuilderOutcome.Nothing; return(null); } }
/// <summary> /// Create a geometry object(s) described by stored face sets, if possible. /// Usually a single-element IList conatining either Solid or Mesh is returned. /// A two-elemant IList containing a Solid as the 1st element and a Mesh as /// the 2nd is returned if while building multiple face sets, a fallback /// was used for some but not all sets. /// </summary> /// <returns>The IList created, or null. The IList can contain a Solid and/or a Mesh. /// If Solid is present, it always the 1st element.</returns> private IList<GeometryObject> CreateGeometryObjects(string guid, out bool hasInvalidData, out TessellatedShapeBuilderOutcome outcome) { m_TessellatedShapeBuilder.CloseConnectedFaceSet(); // The OwnerInfo is currently unused; the value doesn't really matter. m_TessellatedShapeBuilder.LogString = IFCImportFile.TheFileName; m_TessellatedShapeBuilder.LogInteger = IFCImportFile.TheBrepCounter; m_TessellatedShapeBuilder.OwnerInfo = guid != null ? guid : "Temporary Element"; TessellatedShapeBuilderResult result = m_TessellatedShapeBuilder.Build( TargetGeometry, FallbackGeometry, GraphicsStyleId); // It is important that we clear the TSB after we build above, otherwise we will "collect" geometries // in the DirectShape and create huge files with redundant data. m_TessellatedShapeBuilder.Clear(); hasInvalidData = result.HasInvalidData; outcome = result.Outcome; return result.GetGeometricalObjects(); }
/// <summary> /// Create a geometry object(s) described by stored face sets, if possible. /// Usually a single-element IList conatining either Solid or Mesh is returned. /// A two-elemant IList containing a Solid as the 1st element and a Mesh as /// the 2nd is returned if while building multiple face sets, a fallback /// was used for some but not all sets. /// </summary> /// <returns>The IList created, or null. The IList can contain a Solid and/or a Mesh. /// If Solid is present, it always the 1st element.</returns> private IList <GeometryObject> CreateGeometryObjects(string guid, out bool hasInvalidData, out TessellatedShapeBuilderOutcome outcome) { m_TessellatedShapeBuilder.CloseConnectedFaceSet(); // The OwnerInfo is currently unused; the value doesn't really matter. m_TessellatedShapeBuilder.LogString = IFCImportFile.TheFileName; m_TessellatedShapeBuilder.LogInteger = IFCImportFile.TheBrepCounter; m_TessellatedShapeBuilder.OwnerInfo = guid != null ? guid : "Temporary Element"; TessellatedShapeBuilderResult result = m_TessellatedShapeBuilder.Build( TargetGeometry, FallbackGeometry, GraphicsStyleId); // It is important that we clear the TSB after we build above, otherwise we will "collect" geometries // in the DirectShape and create huge files with redundant data. m_TessellatedShapeBuilder.Clear(); hasInvalidData = result.HasInvalidData; outcome = result.Outcome; return(result.GetGeometricalObjects()); }
/// <summary> /// Create a new DirectShape element from given /// list of faces and return the number of faces /// processed. /// Return -1 if a face vertex index exceeds the /// total number of available vertices, /// representing a fatal error. /// </summary> public static DirectShape NewDirectShape( //List<XYZ> vertices, Autodesk.DesignScript.Geometry.Mesh mesh, //FaceCollection faces, //UIDocument UIdoc, //ElementId graphicsStyleId, //string appGuid, string shapeName) { int nFaces = 0; int nFacesFailed = 0; Document doc = DocumentManager.Instance.CurrentDBDocument; string appGuid = doc.Application.ActiveAddInId.GetGUID().ToString(); // Retrieve "<Sketch>" graphics style, // if it exists. FilteredElementCollector collector = new FilteredElementCollector(doc) .OfClass(typeof(GraphicsStyle)); GraphicsStyle style = collector.Cast <GraphicsStyle>() .FirstOrDefault <GraphicsStyle>(gs => gs.Name.Equals("<Sketch>")); ElementId graphicsStyleId = null; if (style != null) { graphicsStyleId = style.Id; } TessellatedShapeBuilder builder = new TessellatedShapeBuilder(); builder.LogString = shapeName; List <Autodesk.DesignScript.Geometry.Point> corners = new List <Autodesk.DesignScript.Geometry.Point>(4); List <XYZ> XYZcorners = new List <XYZ>(4); builder.OpenConnectedFaceSet(false); // foreach (Face f in faces) //{ builder.LogInteger = nFaces; if (corners.Capacity < mesh.FaceIndices.Length) { corners = new List <Autodesk.DesignScript.Geometry.Point>(mesh.FaceIndices.Length); XYZcorners = new List <XYZ>(mesh.FaceIndices.Length); } corners.Clear(); XYZcorners.Clear(); foreach (IndexGroup i in mesh.FaceIndices) { //Debug.Assert(vertices.Count > i.vertex, // "how can the face vertex index be larger " // + "than the total number of vertices?"); corners.Add(mesh.VertexPositions[i.A]); XYZcorners.Add(new XYZ(mesh.VertexPositions[(int)i.A].X, mesh.VertexPositions[(int)i.A].Y, mesh.VertexPositions[(int)i.A].Z)); corners.Add(mesh.VertexPositions[(int)i.B]); XYZcorners.Add(new XYZ(mesh.VertexPositions[(int)i.B].X, mesh.VertexPositions[(int)i.B].Y, mesh.VertexPositions[(int)i.B].Z)); corners.Add(mesh.VertexPositions[(int)i.C]); XYZcorners.Add(new XYZ(mesh.VertexPositions[(int)i.C].X, mesh.VertexPositions[(int)i.C].Y, mesh.VertexPositions[(int)i.C].Z)); if (i.Count > 3) { corners.Add(mesh.VertexPositions[(int)i.D]); XYZcorners.Add(new XYZ(mesh.VertexPositions[(int)i.D].X, mesh.VertexPositions[(int)i.D].Y, mesh.VertexPositions[(int)i.D].Z)); } } try { builder.AddFace(new TessellatedFace(XYZcorners, ElementId.InvalidElementId)); ++nFaces; } catch (Autodesk.Revit.Exceptions.ArgumentException ex) { // Remember something went wrong here. ++nFacesFailed; Debug.Print( "Revit API argument exception {0}\r\n" + "Failed to add face with {1} corners: {2}", ex.Message, corners.Count, string.Join(", ", XYZcorners.Select <XYZ, string>( p => Util.PointString(p)))); } //} builder.CloseConnectedFaceSet(); // Refer to StlImport sample for more clever // handling of target and fallback and the // possible combinations. TessellatedShapeBuilderResult r = builder.Build( TessellatedShapeBuilderTarget.Mesh, TessellatedShapeBuilderFallback.Salvage, graphicsStyleId); ConversionLog myLog = new ConversionLog("c:\\conversionLogFileName.txt"); DataConversionMonitorScope myLoggingScope = new DataConversionMonitorScope(myLog); TessellatedShapeBuilderOutcome testOutcome = r.Outcome; IList <GeometryObject> test = r.GetGeometricalObjects(); try { TransactionManager.Instance.EnsureInTransaction(DocumentManager.Instance.CurrentDBDocument); DirectShape ds = DirectShape.CreateElement( doc, _categoryId, appGuid, shapeName); ds.SetShape(r.GetGeometricalObjects()); ds.Name = shapeName; Debug.Print( "Shape '{0}': added {1} face{2}, {3} face{4} failed.", shapeName, nFaces, Util.PluralSuffix(nFaces), nFacesFailed, Util.PluralSuffix(nFacesFailed)); TransactionManager.Instance.TransactionTaskDone(); return(ds); } catch (Exception ex) { Debug.Print( "Problem with adding DirectShape: " + ex.Message.ToString()); return(null); } }