/// <summary> /// 为面层实体设置颜色、类型等参数 /// </summary> /// <param name="tran"></param> /// <param name="ds"></param> /// <param name="volumn"></param> /// <param name="area"></param> /// <param name="faceOptions"></param> private void SetParameters(Transaction tran, DirectShape ds, double volumn, double area, FaceOptions faceOptions) { // 设置实体对象在指定视力中的填充颜色 OverrideGraphicSettings gs = _view.GetElementOverrides(ds.Id); // 填充颜色 gs.SetProjectionFillColor(faceOptions.Color); // 填充模式 string fillPatternName = "实体填充"; FillPatternElement fpe = FillPatternElement.GetFillPatternElementByName(_doc, FillPatternTarget.Drafting, fillPatternName); if (fpe == null) { fpe = FillPatternElement.Create(_doc, new FillPattern(fillPatternName, FillPatternTarget.Drafting, FillPatternHostOrientation.ToHost)); } gs.SetProjectionFillPatternId(fpe.Id); gs.SetProjectionFillPatternVisible(true); _view.SetElementOverrides(ds.Id, gs); // 设置参数 Parameter p; // 面层对象标识 p = ds.get_Parameter(FaceWallParameters.sp_FaceIdTag_guid); if (p != null) { p.Set(FaceWallParameters.FaceIdentificaion); } // 面积 p = ds.get_Parameter(FaceWallParameters.sp_Area_guid); if (p != null) { p.Set(area); } // 体积 p = ds.get_Parameter(FaceWallParameters.sp_Volumn_guid); if (p != null) { p.Set(volumn); } // 类型 p = ds.get_Parameter(FaceWallParameters.sp_FaceType_guid); if (p != null) { p.Set(faceOptions.FaceType); } }
/// <summary> /// 获取面层类型,如“防水” /// </summary> /// <param name="faceType">提取到的面层类型信息</param> /// <returns>如果没有找到此参数,则返回false。</returns> public bool GetFaceType(out string faceType) { Parameter pa = FaceElement.get_Parameter(FaceWallParameters.sp_FaceType_guid); if (pa == null) { faceType = null; return(false); } faceType = pa.AsString(); return(true); }
/// <summary> /// 获取面层对象的标识符。正常情况下,此参数的值应该是一个常数“CMIE_面层” /// </summary> /// <param name="ds"> </param> /// <param name="wallFace"> 如果此函数返回 true,返回面层对象,如果此函数返回False,则此参数返回 null </param> /// <returns>如果没有找到此参数,则返回false。</returns> public static bool IsWallFace(DirectShape ds, out WallFace wallFace) { if (ds == null) { wallFace = null; return(false); } Parameter pa = ds.get_Parameter(FaceWallParameters.sp_FaceIdTag_guid); if (pa == null) { wallFace = null; return(false); } string idTag = pa.AsString(); if (idTag == null || !pa.AsString().Equals(FaceWallParameters.FaceIdentificaion)) { wallFace = null; return(false); } wallFace = new WallFace(ds); return(true); }
private static void CreateDirectShape(Document doc, Solid solid, Color color, string paramValue) { OverrideGraphicSettings ogs = new OverrideGraphicSettings(); //ogs.SetProjectionFillColor(color); //new Color(0,255,0) //ogs.SetProjectionFillPatternId(new ElementId(4)); //ogs.SetProjectionFillPatternVisible(true); // create direct shape and assign the sphere shape DirectShape dsmax = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel)); dsmax.ApplicationId = "ApplicationID"; dsmax.ApplicationDataId = "ApplicationDataId"; dsmax.SetShape(new GeometryObject[] { solid }); doc.ActiveView.SetElementOverrides(dsmax.Id, ogs); Parameter parameter = dsmax.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS); parameter.Set(paramValue); }
/// <summary> /// 模板生成 /// </summary> /// <param name="doc"></param> /// <param name="face"></param> /// <param name="drt"></param> /// <param name="HostELemID"></param> /// <returns></returns> public static ElementId SurfaceLayerGernerate(Document doc, Face face, DirectShapeType drt, ElementId HostELemID) { DirectShape dsElem = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_Parts), Guid.NewGuid().ToString(), Guid.NewGuid().ToString()); XYZ faceNormal = face.ComputeNormal(new UV(0, 0)); if (face is CylindricalFace) { return(null); //如果是圆柱面则排除 } IList <CurveLoop> faceLoopList = face.GetEdgesAsCurveLoops(); Solid sd = GeometryCreationUtilities.CreateExtrusionGeometry(faceLoopList, faceNormal, 5 / 304.8); //模板尺寸判断,矩形标尺寸,非矩形不标 string tpSize = null; double last = sd.Volume; //catch { SWF.MessageBox.Show(HostELemID.IntegerValue + ""); } //对于生成的solid进行预处理 sd = SolidHandle(doc, HostELemID, sd); double now = sd.Volume; if (now == 0) { return(null); } if (last == now) { if (faceLoopList.Count == 1) { List <Curve> curveList = faceLoopList[0].ToList(); tpSize = bc.RectTangleSize(curveList); } else { tpSize = "非矩形板"; } } else { tpSize = "非矩形板"; } dsElem.SetShape(new List <GeometryObject>() { sd }); dsElem.SetTypeId(drt.Id); dsElem.LookupParameter("HostElemID").Set(HostELemID.IntegerValue); dsElem.LookupParameter("模板面积").Set(sd.Volume / (5 / 304.8)); Parameter dsParamter = dsElem.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS); try { if (faceNormal.IsAlmostEqualTo(-XYZ.BasisZ)) { if (doc.GetElement(HostELemID) is Floor) { dsParamter.Set("楼板底模板"); } else if (doc.GetElement(HostELemID).Category.Id == new ElementId(BuiltInCategory.OST_StructuralFraming)) { dsParamter.Set("梁底模板"); } else if (doc.GetElement(HostELemID) is Wall) { dsParamter.Set("墙洞顶模板"); } else if (doc.GetElement(HostELemID) is Stairs) { dsParamter.Set("平台底模板"); } } else if (faceNormal.IsAlmostEqualTo(XYZ.BasisZ)) { //墙 if (doc.GetElement(HostELemID) is Wall) { dsParamter.Set("墙洞底模板"); } } else { if (doc.GetElement(HostELemID) is Floor) { dsParamter.Set("楼板侧模板"); } else if (doc.GetElement(HostELemID).Category.Id == new ElementId(BuiltInCategory.OST_StructuralFraming)) { dsParamter.Set("梁侧模板"); } else if (doc.GetElement(HostELemID).Category.Id == new ElementId(BuiltInCategory.OST_StructuralColumns)) { dsParamter.Set("柱侧模板"); } else if (doc.GetElement(HostELemID) is Wall) { dsParamter.Set("墙侧模板"); } else if (doc.GetElement(HostELemID) is Stairs) { dsParamter.Set("楼梯侧模板"); } else if (doc.GetElement(HostELemID).Category.Id == new ElementId(BuiltInCategory.OST_StructuralFoundation)) { dsParamter.Set("基础侧模板"); } } } catch (Exception e) { SWF.MessageBox.Show(e.ToString()); } dsElem.LookupParameter("模板尺寸").Set(tpSize); dsElem.LookupParameter("X").Set(faceNormal.X); dsElem.LookupParameter("Y").Set(faceNormal.Y); dsElem.LookupParameter("Z").Set(faceNormal.Z); doc.ActiveView.PartsVisibility = PartsVisibility.ShowPartsOnly; return(dsElem.Id); }
public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; string id_addin = uiapp.ActiveAddInId.GetGUID() .ToString(); IEnumerable <Room> rooms = new FilteredElementCollector(doc) .WhereElementIsNotElementType() .OfClass(typeof(SpatialElement)) .Where(e => e.GetType() == typeof(Room)) .Cast <Room>(); // Collect room data for glTF export List <GltfNodeData> room_data = new List <GltfNodeData>( rooms.Count <Room>()); // Collect geometry data for glTF: a list of // vertex coordinates in millimetres, and a list // of triangle vertex indices into the coord list. List <IntPoint3d> gltf_coords = new List <IntPoint3d>(); List <TriangleIndices> gltf_indices = new List <TriangleIndices>(); using (Transaction tx = new Transaction(doc)) { tx.Start("Generate Direct Shape Elements " + "Representing Room Volumes"); foreach (Room r in rooms) { Debug.Print("Processing " + r.Name + "..."); // Collect data for current room GltfNodeData rd = new GltfNodeData(r); GeometryElement geo = r.ClosedShell; Debug.Assert( geo.First <GeometryObject>() is Solid, "expected a solid for room closed shell"); Solid solid = geo.First <GeometryObject>() as Solid; #region Fix the shape #if FIX_THE_SHAPE_SOMEHOW // Create IList step by step Solid solid = geo.First <GeometryObject>() as Solid; // The room closed shell solid faces have a // non-null graphics style id: // Interior Fill 106074 // The sphere faces' graphics style id is null. // Maybe this graphics style does something // weird in the Forge viewer? // Let's create a copy of the room solid and // see whether that resets the graphics style. solid = SolidUtils.CreateTransformed( solid, Transform.Identity); shape = new GeometryObject[] { solid }; // Create a sphere var center = XYZ.Zero; double radius = 2.0; var p = center + radius * XYZ.BasisY; var q = center - radius * XYZ.BasisY; var profile = new List <Curve>(); profile.Add(Line.CreateBound(p, q)); profile.Add(Arc.Create(q, p, center + radius * XYZ.BasisX)); var curveLoop = CurveLoop.Create(profile); var options = new SolidOptions( ElementId.InvalidElementId, // material ElementId.InvalidElementId); // graphics style var frame = new Frame(center, XYZ.BasisX, -XYZ.BasisZ, XYZ.BasisY); var sphere = GeometryCreationUtilities .CreateRevolvedGeometry(frame, new CurveLoop[] { curveLoop }, 0, 2 * Math.PI, options); shape = new GeometryObject[] { solid, sphere }; #endif // #if FIX_THE_SHAPE_SOMEHOW #endregion // Fix the shape IList <GeometryObject> shape = geo.ToList <GeometryObject>(); // Previous counts define offsets // to new binary data rd.CoordinatesBegin = gltf_coords.Count; rd.TriangleVertexIndicesBegin = gltf_indices.Count; // Create a new solid to use for the direct // shape from the flawed solid returned by // GetClosedShell and gather glTF facet data shape = CopyGeometry(geo, ElementId.InvalidElementId, gltf_coords, gltf_indices); rd.CoordinatesCount = gltf_coords.Count - rd.CoordinatesBegin; rd.TriangleVertexIndexCount = gltf_indices.Count - rd.TriangleVertexIndicesBegin; IEnumerable <IntPoint3d> pts = gltf_coords.Skip <IntPoint3d>( rd.CoordinatesBegin); rd.Min = new IntPoint3d( pts.Min <IntPoint3d, int>(p => p.X), pts.Min <IntPoint3d, int>(p => p.Y), pts.Min <IntPoint3d, int>(p => p.Z)); rd.Max = new IntPoint3d( pts.Max <IntPoint3d, int>(p => p.X), pts.Max <IntPoint3d, int>(p => p.Y), pts.Max <IntPoint3d, int>(p => p.Z)); Dictionary <string, string> param_values = GetParamValues(r); string json = FormatDictAsJson(param_values); DirectShape ds = DirectShape.CreateElement( doc, _id_category_for_direct_shape); ds.ApplicationId = id_addin; ds.ApplicationDataId = r.UniqueId; ds.SetShape(shape); ds.get_Parameter(_bip_properties).Set(json); ds.Name = "Room volume for " + r.Name; room_data.Add(rd); } tx.Commit(); } // Save glTF text and binary data to two files; // metadata, min, max, buffer information; // vertex coordinates and triangle indices // Path.GetTempPath() returns a weird subdirectory // created by Revit, so we will not use that here, e.g., // C:\Users\tammikj\AppData\Local\Temp\bfd59506-2dff-4b0f-bbe4-31587fcaf508 //string path = Path.GetTempPath(); string path = "C:/tmp"; path = Path.Combine(path, doc.Title + "_gltf"); using (StreamWriter s = new StreamWriter( path + ".txt")) { int n = room_data.Count; s.WriteLine("{0} room{1}", n, ((1 == n) ? "" : "s")); s.WriteLine("{0},{1},{2},{3},{4},{5},{6},{7},{8}", "id", // "ElementId", "uid", // "UniqueId", "name", // "RoomName", "min", "max", "coord begin", // "CoordinatesBegin", "count", // "CoordinatesCount", "indices begin", // "TriangleVertexIndicesBegin", "count"); // "TriangleVertexIndexCount" foreach (GltfNodeData rd in room_data) { s.WriteLine(rd.ToString()); } } using (FileStream f = File.Create(path + ".bin")) { using (BinaryWriter writer = new BinaryWriter(f)) { foreach (IntPoint3d p in gltf_coords) { writer.Write((float)p.X); writer.Write((float)p.Y); writer.Write((float)p.Z); } foreach (TriangleIndices ti in gltf_indices) { foreach (int i in ti.Indices) { Debug.Assert(ushort.MaxValue > i, "expected vertex index to fit into unsigned short"); writer.Write((ushort)i); } } } } return(Result.Succeeded); }