/// <summary>
        /// Creates or populates Revit elements based on the information contained in this class.
        /// </summary>
        /// <param name="doc">The document.</param>
        protected override void Create(Document doc)
        {
            DirectShapeType shapeType = Importer.TheCache.UseElementByGUID <DirectShapeType>(doc, GlobalId);

            if (shapeType == null)
            {
                shapeType = IFCElementUtil.CreateElementType(doc, GetVisibleName(), CategoryId, Id);
            }
            else
            {
                // If we used the element from the cache, we want to make sure that the IFCRepresentationMap can access it
                // instead of creating a new element.
                Importer.TheCache.CreatedDirectShapeTypes[Id] = shapeType.Id;
            }

            if (shapeType == null)
            {
                throw new InvalidOperationException("Couldn't create DirectShapeType for IfcTypeObject.");
            }

            m_CreatedElementId = shapeType.Id;

            base.Create(doc);

            TraverseSubElements(doc);
        }
Esempio n. 2
0
        /// <summary>
        /// Create a DirectShapeType.
        /// </summary>
        /// <param name="doc">The document.</param>
        /// <param name="name">The name of the DirectShapeType.</param>
        /// <param name="categoryId">The category of the DirectShape.</param>
        /// <param name="id">The id of the IFCEntity object creating the DirectShape.</param>
        /// <returns>The DirectShape.</returns>
        static public DirectShapeType CreateElementType(Document doc, string name, ElementId categoryId, int id)
        {
            DirectShapeType directShapeType = DirectShapeType.Create(doc, name, IFCElementUtil.GetDSValidCategoryId(doc, categoryId, id));

            Importer.TheCache.CreatedDirectShapeTypes[id] = directShapeType.Id;
            return(directShapeType);
        }
Esempio n. 3
0
        public static List <Revit.Elements.Element> MassingsByRooms(List <Revit.Elements.Element> Rooms)
        {
            var doc        = DocumentManager.Instance.CurrentDBDocument;
            var MassBICObj = (BuiltInParameter)System.Enum.ToObject(typeof(BuiltInCategory), BuiltInCategory.OST_Mass);
            var objId      = new ElementId(MassBICObj);

            TransactionManager.Instance.EnsureInTransaction(doc);
            var Massings = new List <Revit.Elements.Element>();

            foreach (var room in Rooms)
            {
                var name   = room.InternalElement.LookupParameter("Name").AsString();
                var number = room.InternalElement.LookupParameter("Number").AsString();

                var elem_name  = number + "_" + name;
                var geoobj     = room.InternalElement.get_Geometry(new Options());
                var get_enum   = geoobj.GetEnumerator();
                var next       = get_enum.MoveNext();
                var shape      = get_enum.Current;
                var shape_list = new List <GeometryObject>();
                shape_list.Add(shape);
                var shapeType = DirectShapeType.Create(doc, elem_name, objId);
                shapeType.SetShape(shape_list);
                var lib = DirectShapeLibrary.GetDirectShapeLibrary(doc);
                lib.AddDefinitionType(elem_name, shapeType.Id);
                var element = Autodesk.Revit.DB.DirectShape.CreateElementInstance(doc, shapeType.Id, objId, elem_name, Transform.Identity);
                element.SetTypeId(shapeType.Id);

                Massings.Add(element.ToDSType(true));
            }

            TransactionManager.Instance.TransactionTaskDone();
            return(Massings);
        }
Esempio n. 4
0
        /// <summary>
        /// Create a DirectShapeType.
        /// </summary>
        /// <param name="doc">The document.</param>
        /// <param name="name">The name of the DirectShapeType.</param>
        /// <param name="categoryId">The category of the DirectShape.</param>
        /// <param name="id">The id of the IFCEntity object creating the DirectShape.</param>
        /// <param name="guid">The guid of the IFCEntity object creating the DirectShape.</param>
        /// <returns>The DirectShape.</returns>
        static public DirectShapeType CreateElementType(Document doc, string name, ElementId categoryId, int id, string guid, IFCEntityType entityType)
        {
            DirectShapeTypeOptions options = new DirectShapeTypeOptions();

            options.AllowDuplicateNames = true;
            ElementId       validCategoryId = IFCElementUtil.GetDSValidCategoryId(doc, categoryId, id);
            DirectShapeType directShapeType = DirectShapeType.Create(doc, name, validCategoryId, options);

            Importer.TheCache.CreatedDirectShapeTypes[id] = directShapeType.Id;

            Importer.TheProcessor.CreateElementType(id, guid, entityType.ToString(), validCategoryId.IntegerValue);

            return(directShapeType);
        }
Esempio n. 5
0
        /// <summary>
        /// Creates or populates Revit elements based on the information contained in this class.
        /// </summary>
        /// <param name="doc">The document.</param>
        protected override void Create(Document doc)
        {
            DirectShapeType shapeType = Importer.TheCache.UseElementByGUID<DirectShapeType>(doc, GlobalId);

            if (shapeType == null)
                shapeType = DirectShapeType.Create(doc, GetName("Direct Shape Type"), CategoryId);

            if (shapeType == null)
                throw new InvalidOperationException("Couldn't create DirectShapeType for IfcTypeObject.");

            m_CreatedElementId = shapeType.Id;

            base.Create(doc);

            TraverseSubElements(doc);
        }
Esempio n. 6
0
 /// <summary>
 /// Set the plan view representation of the given DirectShape or DirectShapeType given the information created by AddPlanViewCurves.
 /// </summary>
 /// <param name="shape">The DirectShape or DirectShapeType.</param>
 public void SetPlanViewRep(Element shape)
 {
     if (m_ViewShapeBuilder != null)
     {
         if (shape is DirectShape)
         {
             DirectShape ds = shape as DirectShape;
             ds.SetShape(m_ViewShapeBuilder);
         }
         else if (shape is DirectShapeType)
         {
             DirectShapeType dst = shape as DirectShapeType;
             dst.SetShape(m_ViewShapeBuilder);
         }
         else
         {
             throw new ArgumentException("SetPlanViewRep only works on DirectShape and DirectShapeType.");
         }
     }
 }
Esempio n. 7
0
        private static void CreateDirectShape(Document doc, IList <GeometryObject> geometry)
        {
            DirectShapeLibrary directShapeLibrary = DirectShapeLibrary.GetDirectShapeLibrary(doc);
            DirectShapeType    directShapeType    = DirectShapeType.Create(doc, "NavisWorksShape", new ElementId(BuiltInCategory.OST_GenericModel));

            directShapeType.SetShape(geometry);
            directShapeLibrary.AddDefinitionType("NavisWorksShape", directShapeType.Id);

            DirectShape ds = DirectShape.CreateElementInstance(doc, directShapeType.Id, directShapeType.Category.Id, "NavisWorksShape", Transform.Identity);

            ds.SetTypeId(directShapeType.Id);
            ds.ApplicationId     = Assembly.GetExecutingAssembly().GetType().GUID.ToString();
            ds.ApplicationDataId = Guid.NewGuid().ToString();

            DirectShapeOptions dsOptions = ds.GetOptions();

            dsOptions.ReferencingOption = DirectShapeReferencingOption.Referenceable;
            ds.SetOptions(dsOptions);

            ds.SetShape(geometry);
        }
Esempio n. 8
0
        public static Result Import3DMFile(string filePath, Document doc, BuiltInCategory builtInCategory)
        {
            try
            {
                DirectShapeLibrary.GetDirectShapeLibrary(doc).Reset();

                using (var model = File3dm.Read(filePath))
                {
                    var scaleFactor = RhinoMath.UnitScale(model.Settings.ModelUnitSystem, Revit.ModelUnitSystem);

                    using (var trans = new Transaction(doc, "Import 3D Model"))
                    {
                        if (trans.Start() == TransactionStatus.Started)
                        {
                            var categoryId = new ElementId(builtInCategory);
                            var materials  = GetMaterialsByName(doc);

                            var type = DirectShapeType.Create(doc, Path.GetFileName(filePath), categoryId);

                            foreach (var obj in model.Objects.Where(x => !x.Attributes.IsInstanceDefinitionObject && x.Attributes.Space == ActiveSpace.ModelSpace))
                            {
                                if (!obj.Attributes.Visible)
                                {
                                    continue;
                                }

                                var geometryList = ImportObject(model, obj.Geometry, obj.Attributes, doc, materials, scaleFactor).ToArray();
                                if (geometryList == null)
                                {
                                    continue;
                                }

                                try { type.AppendShape(geometryList); }
                                catch (Autodesk.Revit.Exceptions.ArgumentException) { }
                            }

                            var ds = DirectShape.CreateElement(doc, type.Category.Id);
                            ds.SetTypeId(type.Id);

                            var library = DirectShapeLibrary.GetDirectShapeLibrary(doc);
                            if (!library.ContainsType(type.UniqueId))
                            {
                                library.AddDefinitionType(type.UniqueId, type.Id);
                            }

                            ds.SetShape(DirectShape.CreateGeometryInstance(doc, type.UniqueId, Autodesk.Revit.DB.Transform.Identity));

                            if (trans.Commit() == TransactionStatus.Committed)
                            {
                                var elements = new ElementId[] { ds.Id };
                                Revit.ActiveUIDocument.Selection.SetElementIds(elements);
                                Revit.ActiveUIDocument.ShowElements(elements);

                                return(Result.Succeeded);
                            }
                        }
                    }
                }
            }
            finally
            {
                DirectShapeLibrary.GetDirectShapeLibrary(doc).Reset();
            }

            return(Result.Failed);
        }
Esempio n. 9
0
        /// <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);
        }
Esempio n. 10
0
        /// <summary>
        /// 主体模板生成
        /// </summary>
        /// <param name="hostElemList">构件List</param>
        /// <param name="normalList">排除面的法向</param>
        /// <returns>生成有问题的构件的id</returns>
        public static string HostElemTpGenerate(List <Element> hostElemList, DirectShapeType dst, List <XYZ> normalList)
        {
            Document         doc           = hostElemList[0].Document;
            List <Solid>     solidList     = new List <Solid>();
            List <ElementId> failureIdList = new List <ElementId>();
            string           failureTxt    = null;

            foreach (Element hostElem in hostElemList)
            {
                if (hostElem is Stairs)
                {
                    solidList.Add(bc.AllUnionSolid(hostElem));
                }
                else if (hostElem.Category.Id == new ElementId(BuiltInCategory.OST_StructuralFoundation))
                {
                    solidList.Add(bc.AllSolid_Of_Element(hostElem).OrderBy(m => m.Volume).Last());
                }
                else if (hostElem.Category.Id == new ElementId(BuiltInCategory.OST_StructuralFraming))
                {
                    try
                    {
                        LocationCurve Curve = (hostElem as FamilyInstance).Location as LocationCurve;
                        solidList = bc.AllSolid_Of_Element(hostElem);
                    }
                    catch
                    {
                        failureTxt += hostElem.Id.IntegerValue.ToString() + "\r\n";
                        continue;
                    }
                }
                else
                {
                    solidList = bc.AllSolid_Of_Element(hostElem);
                }
                foreach (Solid sd in solidList)
                {
                    foreach (Face face in sd.Faces)
                    {
                        XYZ faceNormal = face.ComputeNormal(new UV(0, 0));
                        if (normalList.Where(m => m.IsAlmostEqualTo(faceNormal)).Count() > 0)
                        {
                            continue;
                        }
                        try
                        {
                            bc.SurfaceLayerGernerate(doc, face, dst, hostElem.Id);
                        }
                        catch
                        {
                            if (failureIdList.Count == 0 || failureIdList.Where(m => m.IntegerValue == hostElem.Id.IntegerValue).Count() == 0)
                            {
                                failureIdList.Add(hostElem.Id);
                            }
                        }
                    }
                }
            }
            if (failureIdList.Count != 0)
            {
                failureIdList.ConvertAll(m => failureTxt += m.IntegerValue + "\r\n");
            }
            return(failureTxt);
        }
Esempio n. 11
0
        /// <summary>
        /// Create geometry for a particular representation map.
        /// </summary>
        /// <param name="shapeEditScope">The geometry creation scope.</param>
        /// <param name="lcs">Local coordinate system for the geometry, without scale.</param>
        /// <param name="scaledLcs">Local coordinate system for the geometry, including scale, potentially non-uniform.</param>
        /// <remarks>For this function, if lcs is null, we will create a library item for the geometry.</remarks>
        public void CreateShape(IFCImportShapeEditScope shapeEditScope, Transform lcs, Transform scaledLcs, string guid)
        {
            bool creatingLibraryDefinition = (lcs == null);

            if (MappedRepresentation != null)
            {
                // Look for cached shape; if found, return.
                if (creatingLibraryDefinition)
                {
                    if (IFCImportFile.TheFile.ShapeLibrary.FindDefinitionType(Id.ToString()) != ElementId.InvalidElementId)
                    {
                        return;
                    }
                }

                Transform mappingTransform = null;
                if (lcs == null)
                {
                    mappingTransform = MappingOrigin;
                }
                else
                {
                    if (MappingOrigin == null)
                    {
                        mappingTransform = lcs;
                    }
                    else
                    {
                        mappingTransform = lcs.Multiply(MappingOrigin);
                    }
                }

                Transform scaledMappingTransform = null;
                if (scaledLcs == null)
                {
                    scaledMappingTransform = mappingTransform;
                }
                else
                {
                    if (MappingOrigin == null)
                    {
                        scaledMappingTransform = scaledLcs;
                    }
                    else
                    {
                        scaledMappingTransform = scaledLcs.Multiply(MappingOrigin);
                    }
                }

                int numExistingSolids = shapeEditScope.Creator.Solids.Count;
                int numExistingCurves = shapeEditScope.Creator.FootprintCurves.Count;

                MappedRepresentation.CreateShape(shapeEditScope, mappingTransform, scaledMappingTransform, guid);

                if (creatingLibraryDefinition)
                {
                    int numNewSolids = shapeEditScope.Creator.Solids.Count;
                    int numNewCurves = shapeEditScope.Creator.FootprintCurves.Count;

                    if ((numExistingSolids != numNewSolids) || (numExistingCurves != numNewCurves))
                    {
                        IList <GeometryObject> mappedSolids = new List <GeometryObject>();
                        for (int ii = numExistingSolids; ii < numNewSolids; ii++)
                        {
                            mappedSolids.Add(shapeEditScope.Creator.Solids[numExistingSolids].GeometryObject);
                            shapeEditScope.Creator.Solids.RemoveAt(numExistingSolids);
                        }

                        IList <Curve> mappedCurves = new List <Curve>();
                        for (int ii = numExistingCurves; ii < numNewCurves; ii++)
                        {
                            mappedCurves.Add(shapeEditScope.Creator.FootprintCurves[numExistingCurves]);
                            shapeEditScope.Creator.FootprintCurves.RemoveAt(numExistingCurves);
                        }
                        shapeEditScope.AddPlanViewCurves(mappedCurves, Id);

                        //IFCImportFile.TheFile.ShapeLibrary.AddDefinition(Id.ToString(), mappedItems);

                        Document        doc             = IFCImportFile.TheFile.Document;
                        DirectShapeType directShapeType = DirectShapeType.Create(doc, Id.ToString(), shapeEditScope.CategoryId);
                        directShapeType.SetShape(mappedSolids);
                        shapeEditScope.SetPlanViewRep(directShapeType);

                        IFCImportFile.TheFile.ShapeLibrary.AddDefinitionType(Id.ToString(), directShapeType.Id);
                    }
                }
            }
        }
        /// <summary>
        /// Create geometry for a particular representation map.
        /// </summary>
        /// <param name="shapeEditScope">The geometry creation scope.</param>
        /// <param name="lcs">Local coordinate system for the geometry, without scale.</param>
        /// <param name="scaledLcs">Local coordinate system for the geometry, including scale, potentially non-uniform.</param>
        /// <remarks>For this function, if lcs is null, we will create a library item for the geometry.</remarks>
        public void CreateShape(IFCImportShapeEditScope shapeEditScope, Transform lcs, Transform scaledLcs, string guid)
        {
            bool creatingLibraryDefinition = (lcs == null);

            if (MappedRepresentation != null)
            {
                // Look for cached shape; if found, return.
                if (creatingLibraryDefinition)
                {
                    if (IFCImportFile.TheFile.ShapeLibrary.FindDefinitionType(Id.ToString()) != ElementId.InvalidElementId)
                    {
                        return;
                    }
                }

                Transform mappingTransform = null;
                if (lcs == null)
                {
                    mappingTransform = MappingOrigin;
                }
                else
                {
                    if (MappingOrigin == null)
                    {
                        mappingTransform = lcs;
                    }
                    else
                    {
                        mappingTransform = lcs.Multiply(MappingOrigin);
                    }
                }

                Transform scaledMappingTransform = null;
                if (scaledLcs == null)
                {
                    scaledMappingTransform = mappingTransform;
                }
                else
                {
                    if (MappingOrigin == null)
                    {
                        scaledMappingTransform = scaledLcs;
                    }
                    else
                    {
                        scaledMappingTransform = scaledLcs.Multiply(MappingOrigin);
                    }
                }

                int numExistingSolids = shapeEditScope.Creator.Solids.Count;
                int numExistingCurves = shapeEditScope.Creator.FootprintCurves.Count;

                MappedRepresentation.CreateShape(shapeEditScope, mappingTransform, scaledMappingTransform, guid);

                if (creatingLibraryDefinition)
                {
                    int numNewSolids = shapeEditScope.Creator.Solids.Count;
                    int numNewCurves = shapeEditScope.Creator.FootprintCurves.Count;

                    if ((numExistingSolids != numNewSolids) || (numExistingCurves != numNewCurves))
                    {
                        IList <GeometryObject> mappedSolids = new List <GeometryObject>();
                        for (int ii = numExistingSolids; ii < numNewSolids; ii++)
                        {
                            mappedSolids.Add(shapeEditScope.Creator.Solids[numExistingSolids].GeometryObject);
                            shapeEditScope.Creator.Solids.RemoveAt(numExistingSolids);
                        }

                        IList <Curve> mappedCurves = new List <Curve>();
                        for (int ii = numExistingCurves; ii < numNewCurves; ii++)
                        {
                            mappedCurves.Add(shapeEditScope.Creator.FootprintCurves[numExistingCurves]);
                            shapeEditScope.Creator.FootprintCurves.RemoveAt(numExistingCurves);
                        }
                        shapeEditScope.AddPlanViewCurves(mappedCurves, Id);

                        Document        doc             = IFCImportFile.TheFile.Document;
                        DirectShapeType directShapeType = null;

                        IFCTypeProduct typeProduct = null;
                        if (Importer.TheCache.RepMapToTypeProduct.TryGetValue(Id, out typeProduct))
                        {
                            ElementId directShapeTypeId = ElementId.InvalidElementId;
                            if (Importer.TheCache.CreatedDirectShapeTypes.TryGetValue(typeProduct.Id, out directShapeTypeId))
                            {
                                directShapeType = doc.GetElement(directShapeTypeId) as DirectShapeType;
                            }
                        }

                        if (directShapeType == null)
                        {
                            string directShapeTypeName = Id.ToString();
                            directShapeType = IFCElementUtil.CreateElementType(doc, directShapeTypeName, shapeEditScope.CategoryId, Id);
                        }

                        // Note that this assumes that there is only one 2D rep per DirectShapeType.
                        directShapeType.AppendShape(mappedSolids);
                        if (mappedCurves.Count != 0)
                        {
                            shapeEditScope.SetPlanViewRep(directShapeType);
                        }

                        IFCImportFile.TheFile.ShapeLibrary.AddDefinitionType(Id.ToString(), directShapeType.Id);
                    }
                }
            }
        }
Esempio n. 13
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument      uidoc = commandData.Application.ActiveUIDocument;
            Application     app   = commandData.Application.Application;
            Document        doc   = uidoc.Document;
            DefinitionFile  ds    = app.OpenSharedParameterFile();
            DirectShapeType dst   = null;
            TpWin           Tpwin = new TpWin();

            if (Tpwin.ShowDialog() == false)
            {
                return(Result.Succeeded);
            }
            List <string> checkOpt = Tpwin.chbStrList;

            using (TransactionGroup transGroup = new TransactionGroup(doc, "模板创建"))
            {
                if (transGroup.Start() == TransactionStatus.Started)
                {
                    Transaction transShare = new Transaction(doc, "创建共享参数");
                    transShare.Start();
                    bool b = bc.ShareParameterGenerate(doc, app);
                    transShare.Commit();
                    if (b == false)
                    {
                        transGroup.RollBack();
                    }
                    if (bc.FilterElementList <DirectShapeType>(doc).Where(m => m.Name == "模板").Count() == 0)
                    {
                        Transaction transCreat = new Transaction(doc, "创建类型");
                        transCreat.Start();
                        dst = DirectShapeType.Create(doc, "模板", new ElementId(BuiltInCategory.OST_Parts));
                        transCreat.Commit();
                    }
                    else
                    {
                        dst = bc.FilterElementList <DirectShapeType>(doc).Where(m => m.Name == "模板").First() as DirectShapeType;
                    }
                    List <Element> floorList = bc.FilterElementList <Floor>(doc);
                    List <Element> beamList  = bc.FilterElementList <FamilyInstance>(doc, BuiltInCategory.OST_StructuralFraming);
                    List <Element> colList   = bc.FilterElementList <FamilyInstance>(doc, BuiltInCategory.OST_StructuralColumns);
                    List <Element> walList   = bc.FilterElementList <Wall>(doc, BuiltInCategory.OST_Walls).Where(m => m.Name.Contains("DW")).Count() > 0?
                                               bc.FilterElementList <Wall>(doc, BuiltInCategory.OST_Walls).Where(m => m.Name.Contains("DW")).ToList():new List <Element>();
                    List <Element> stairsList = bc.FilterElementList <Stairs>(doc, BuiltInCategory.OST_Stairs).ToList();
                    List <Element> basisList  = bc.FilterElementList <FamilyInstance>(doc, BuiltInCategory.OST_StructuralFoundation);
                    //SWF.MessageBox.Show(stairsList.Count.ToString());
                    string      failureElemIds = null;
                    Transaction trans          = new Transaction(doc, "创建模板");
                    trans.Start();
                    //楼板
                    if (checkOpt.Contains("板模板"))
                    {
                        string flTxt = bc.HostElemTpGenerate(floorList, dst, new List <XYZ>()
                        {
                            XYZ.BasisZ
                        });
                        if (flTxt != null)
                        {
                            failureElemIds += "楼板" + "\r\n" + flTxt;
                        }
                    }
                    //梁模板
                    if (checkOpt.Contains("梁模板"))
                    {
                        string beamTxt = bc.HostElemTpGenerate(beamList, dst, new List <XYZ>()
                        {
                            XYZ.BasisZ
                        });
                        if (beamTxt != null)
                        {
                            failureElemIds += "梁" + "\r\n" + beamTxt;
                        }
                    }
                    //柱模板
                    if (checkOpt.Contains("柱模板"))
                    {
                        string colTxt = bc.HostElemTpGenerate(colList, dst, new List <XYZ>()
                        {
                            XYZ.BasisZ, -XYZ.BasisZ
                        });
                        if (colTxt != null)
                        {
                            failureElemIds += "柱" + "\r\n" + colTxt;
                        }
                    }
                    //墙模板
                    foreach (Wall wal in walList)
                    {
                        List <ElementId> failureWallIds = new List <ElementId>();
                        if (!checkOpt.Contains("墙模板"))
                        {
                            break;
                        }
                        IList <Solid> walSolidList = bc.AllSolid_Of_Element(wal);
                        foreach (Solid sd in walSolidList)
                        {
                            List <Face> faceList = sd.Faces.Cast <Face>().ToList();
                            //获取高度最小值
                            double walBottom = double.MaxValue;
                            double walHeight = double.MinValue;
                            faceList.ConvertAll(m =>
                            {
                                BoundingBoxUV bdxyz = m.GetBoundingBox();
                                XYZ min             = m.Evaluate(bdxyz.Min);
                                XYZ max             = m.Evaluate(bdxyz.Max);
                                double minV         = min.Z < max.Z ? min.Z : max.Z;
                                double maxV         = min.Z > max.Z ? min.Z : max.Z;
                                walBottom           = walBottom < minV ? walBottom : minV;
                                walHeight           = walHeight > maxV ? walHeight : maxV;
                                return(true);
                            });

                            foreach (Face wface in sd.Faces)
                            {
                                XYZ    wfaceNormal  = wface.ComputeNormal(new UV(0, 0));
                                double faceElvation = wface.Evaluate(new UV(0, 0)).Z;
                                if (wfaceNormal.IsAlmostEqualTo(XYZ.BasisZ) && (walHeight - faceElvation) * 304.8 < 10)
                                {
                                    continue;
                                }
                                if (wfaceNormal.IsAlmostEqualTo(-XYZ.BasisZ) && (faceElvation - walBottom) * 304.8 < 10)
                                {
                                    continue;
                                }
                                try
                                {
                                    bc.SurfaceLayerGernerate(doc, wface, dst, wal.Id);
                                }
                                catch
                                {
                                    if (failureWallIds.Count == 0 || failureWallIds.Where(m => m.IntegerValue == wal.Id.IntegerValue).Count() == 0)
                                    {
                                        failureWallIds.Add(wal.Id);
                                    }
                                }
                            }
                            if (failureWallIds.Count != 0)
                            {
                                failureElemIds += "墙" + "\r\n";
                                failureWallIds.ConvertAll(m => m.IntegerValue.ToString() + "\r\n");
                            }
                        }
                    }
                    //楼梯模板
                    if (checkOpt.Contains("楼梯模板"))
                    {
                        string stairsTxt = bc.HostElemTpGenerate(stairsList, dst, new List <XYZ>()
                        {
                            XYZ.BasisZ
                        });
                        if (stairsTxt != null)
                        {
                            failureElemIds += "楼梯" + "\r\n" + stairsTxt;
                        }
                    }
                    //基础模板
                    if (checkOpt.Contains("基础模板"))
                    {
                        string basisTxt = bc.HostElemTpGenerate(basisList, dst, new List <XYZ>()
                        {
                            XYZ.BasisZ, -XYZ.BasisZ
                        });
                        if (basisTxt != null)
                        {
                            failureElemIds += "基础" + "\r\n" + basisTxt;
                        }
                    }
                    trans.Commit();
                    if (failureElemIds != null)
                    {
                        SWF.SaveFileDialog sfd = new SWF.SaveFileDialog();
                        sfd.Filter       = "文本文档|*.txt"; //删选、设定文件显示类型
                        sfd.DefaultExt   = ".txt";
                        sfd.FileName     = "模板生成错误构件ID";
                        sfd.AddExtension = true;
                        sfd.ShowDialog();
                        string path = sfd.FileName;
                        File.WriteAllText(@"C: \Users\BXS\Desktop\FailureText.txt", failureElemIds);
                    }
                }
                transGroup.Assimilate();
            }
            return(Result.Succeeded);
        }
Esempio n. 14
0
        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;
            View          activeView = doc.ActiveView;

            List <Element> col = new List <Element>();

            Reference r = uidoc.Selection.PickObject(ObjectType.Element, "Select Element");

            col.Add(doc.GetElement(r));

            Options geometryOptions = new Options();

            ElementId cat1Id = new ElementId(BuiltInCategory.OST_Walls);


            DirectShapeLibrary dsLib = DirectShapeLibrary.GetDirectShapeLibrary(doc);

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Convert elements to DirectShapes");

                foreach (Element e in col)
                {
                    GeometryElement gelt = e.get_Geometry(
                        geometryOptions);

                    if (null != gelt)
                    {
                        string appDataGUID = e.Id.ToString();

                        try
                        {
                            string          familyName = "MyFamily";
                            DirectShapeType dsType1    = DirectShapeType.Create(doc, familyName, cat1Id);
                            dsType1.SetShape(new List <GeometryObject>(gelt));
                            dsLib.AddDefinitionType(familyName, dsType1.Id);

                            Transform trs = Transform.Identity;

                            DirectShape ds1 = DirectShape.CreateElementInstance(doc, dsType1.Id, cat1Id, familyName, trs);

                            doc.Delete(e.Id);

                            TaskDialog.Show("Result", "Element Flattened");
                        }
                        catch (Exception ex)
                        {
                            TaskDialog.Show("Error", ex.Message);
                        }
                    }
                }
                tx.Commit();
            }

            return(Result.Succeeded);
        }