예제 #1
0
 //----------------------------------------------------------
 public void ChangeWallType(UIApplication uiapp, Document doc)
 {
     try
     {
         List <string> modelWallType   = new List <string>();
         List <string> qualityWallType = new List <string>();
         var           wallType        = new FilteredElementCollector(doc).OfClass(typeof(WallType)).ToElements();
         foreach (WallType type in wallType)
         {
             if (type.Name.Split('_').Count() > 0 && type.Name.Split('_')[0] == "S")
             {
                 try
                 {
                     CompoundStructure compound = null;
                     compound = type.GetCompoundStructure();
                     IList <CompoundStructureLayer> getlayer = compound.GetLayers();
                     ElementId materialId = new ElementId(-1);
                     double    thickness  = 0;
                     foreach (var layer in getlayer)
                     {
                         if (layer.Function == MaterialFunctionAssignment.Structure)
                         {
                             materialId = layer.MaterialId;
                             thickness  = layer.Width;
                         }
                     }
                     //-----------------------------------------------------------------------
                     var countLayer = compound.LayerCount;
                     var layers     = compound.GetLayers();
                     for (var i = 0; i < countLayer; i++)
                     {
                         layers.RemoveAt(0);
                     }
                     layers.Add(new CompoundStructureLayer(thickness, MaterialFunctionAssignment.Structure, materialId));
                     compound.SetLayers(layers);
                     type.SetCompoundStructure(compound);
                 }
                 catch (Exception)
                 {
                 }
             }
         }
     }
     catch (Exception)
     {
     }
 }
예제 #2
0
        /// <summary>
        /// Create vertical CompoundStructure for wall: new layers, split new region, new sweep and new reveal.
        /// </summary>
        /// <param name="wall">The wall applying the new CompoundStructure.</param>
        public void CreateCSforWall(Wall wall)
        {
            // Get CompoundStructure


            WallType wallType = wall.WallType;
            //wallType.Name = wallType.Name + "_WithNewCompoundStructure";
            CompoundStructure wallCS = wallType.GetCompoundStructure();
            // Get material for CompoundStructureLayer

            Material masonry_Brick = CreateSampleBrickMaterial();
            Material concrete      = CreateSampleConcreteMaterial();

            // Create CompoundStructureLayers and add the materials created above to them.
            List <CompoundStructureLayer> csLayers       = new List <CompoundStructureLayer>();
            CompoundStructureLayer        finish1Layer   = new CompoundStructureLayer(0.2, MaterialFunctionAssignment.Finish1, masonry_Brick.Id);
            CompoundStructureLayer        substrateLayer = new CompoundStructureLayer(0.1, MaterialFunctionAssignment.Substrate, ElementId.InvalidElementId);
            CompoundStructureLayer        structureLayer = new CompoundStructureLayer(0.5, MaterialFunctionAssignment.Structure, concrete.Id);
            CompoundStructureLayer        membraneLayer  = new CompoundStructureLayer(0, MaterialFunctionAssignment.Membrane, ElementId.InvalidElementId);
            CompoundStructureLayer        finish2Layer   = new CompoundStructureLayer(0.2, MaterialFunctionAssignment.Finish2, concrete.Id);

            csLayers.Add(finish1Layer);
            csLayers.Add(substrateLayer);
            csLayers.Add(structureLayer);
            csLayers.Add(membraneLayer);
            csLayers.Add(finish2Layer);

            // Set the created layers to CompoundStructureLayer
            wallCS.SetLayers(csLayers);


            //Set which layer is used for structural analysis
            wallCS.StructuralMaterialIndex = 2;

            // Set shell layers and wrapping.
            wallCS.SetNumberOfShellLayers(ShellLayerType.Interior, 2);
            wallCS.SetNumberOfShellLayers(ShellLayerType.Exterior, 1);
            wallCS.SetParticipatesInWrapping(0, false);

            // Points for adding wall sweep and reveal.
            UV sweepPoint  = UV.Zero;
            UV revealPoint = UV.Zero;

            // split the region containing segment 0.
            int segId = wallCS.GetSegmentIds()[0];

            foreach (int regionId in wallCS.GetAdjacentRegions(segId))
            {
                // Get the end points of segment 0.
                UV endPoint1 = UV.Zero;
                UV endPoint2 = UV.Zero;
                wallCS.GetSegmentEndPoints(segId, regionId, out endPoint1, out endPoint2);

                // Split a new region in split point and orientation.
                RectangularGridSegmentOrientation splitOrientation = (RectangularGridSegmentOrientation)(((int)(wallCS.GetSegmentOrientation(segId)) + 1) % 2);
                UV   splitUV         = (endPoint1 + endPoint2) / 2.0;
                int  newRegionId     = wallCS.SplitRegion(splitUV, splitOrientation);
                bool isValidRegionId = wallCS.IsValidRegionId(newRegionId);

                // Find the enclosing region and the two segments intersected by a line through the split point
                int segId1;
                int segId2;
                int findRegionId = wallCS.FindEnclosingRegionAndSegments(splitUV, splitOrientation, out segId1, out segId2);

                // Get the end points of finding segment 1 and compute the wall sweep point.
                UV eP1 = UV.Zero;
                UV eP2 = UV.Zero;
                wallCS.GetSegmentEndPoints(segId1, findRegionId, out eP1, out eP2);
                sweepPoint = (eP1 + eP2) / 4.0;

                // Get the end points of finding segment 2 and compute the wall reveal point.
                UV ep3 = UV.Zero;
                UV ep4 = UV.Zero;
                wallCS.GetSegmentEndPoints(segId2, findRegionId, out ep3, out ep4);
                revealPoint = (ep3 + ep4) / 2.0;
            }

            // Create a WallSweepInfo for wall sweep
            WallSweepInfo sweepInfo = new WallSweepInfo(true, WallSweepType.Sweep);

            PrepareWallSweepInfo(sweepInfo, sweepPoint.V);
            // Set sweep profile: Sill-Precast : 8" Wide
            sweepInfo.ProfileId = GetProfile("8\" Wide").Id;
            sweepInfo.Id        = 101;
            wallCS.AddWallSweep(sweepInfo);

            // Create a WallSweepInfo for wall reveal
            WallSweepInfo revealInfo = new WallSweepInfo(true, WallSweepType.Reveal);

            PrepareWallSweepInfo(revealInfo, revealPoint.U);
            revealInfo.Id = 102;
            wallCS.AddWallSweep(revealInfo);

            // Set the new wall CompoundStructure to the type of wall.
            wallType.SetCompoundStructure(wallCS);
        }
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication app = commandData.Application;
            Document      doc = app.ActiveUIDocument.Document;

#if _2011
            //
            // code for the Revit 2011 API:
            //
            Debug.Assert(false,
                         "Currently, no new wall layer can be created, because"
                         + "there is no creation method available for it.");

            foreach (WallType wallType in doc.WallTypes)
            {
                if (0 < wallType.CompoundStructure.Layers.Size)
                {
                    CompoundStructureLayer oldLayer
                        = wallType.CompoundStructure.Layers.get_Item(0);

                    WallType newWallType
                        = wallType.Duplicate("NewWallType") as WallType;

                    CompoundStructure structure
                        = newWallType.CompoundStructure;

                    CompoundStructureLayerArray layers
                        = structure.Layers;


                    // from here on, nothing works, as expected:
                    // in the Revir 2010 API, we could call the constructor
                    // even though it is for internal use only.
                    // in 2011, it is not possible to call it either.

                    CompoundStructureLayer newLayer = null;
                    //  = new CompoundStructureLayer(); // for internal use only

                    newLayer.DeckProfile = oldLayer.DeckProfile;
                    //newLayer.DeckUsage = oldLayer.DeckUsage; // read-only
                    //newLayer.Function = oldLayer.Function; // read-only
                    newLayer.Material  = oldLayer.Material;
                    newLayer.Thickness = oldLayer.Thickness;
                    newLayer.Variable  = oldLayer.Variable;
                    layers.Append(newLayer);
                }
            }
#endif // _2011

            //WallTypeSet wallTypes = doc.WallTypes; // 2013

            FilteredElementCollector wallTypes
                = new FilteredElementCollector(doc)
                  .OfClass(typeof(WallType)); // 2014

            foreach (WallType wallType in wallTypes)
            {
                if (0 < wallType.GetCompoundStructure().GetLayers().Count)
                {
                    CompoundStructureLayer oldLayer
                        = wallType.GetCompoundStructure().GetLayers()[0];

                    WallType newWallType
                        = wallType.Duplicate("NewWallType") as WallType;

                    CompoundStructure structure
                        = newWallType.GetCompoundStructure();

                    IList <CompoundStructureLayer> layers
                        = structure.GetLayers();

                    // in Revit 2012, we can create a new layer:

                    double width = 0.1;
                    MaterialFunctionAssignment function = oldLayer.Function;
                    ElementId materialId = oldLayer.MaterialId;

                    CompoundStructureLayer newLayer
                        = new CompoundStructureLayer(width, function, materialId);

                    layers.Add(newLayer);
                    structure.SetLayers(layers);
                    newWallType.SetCompoundStructure(structure);
                }
            }
            return(Result.Succeeded);
        }
예제 #4
0
파일: MyRevit.cs 프로젝트: pgrandin/revit
        public Result setup_wall_struct(Document doc)
        {
            WallType wallType = null;

            // FIXME : replace with the material approach?
            FilteredElementCollector wallTypes
                = new FilteredElementCollector(doc)
                  .OfClass(typeof(WallType));

            foreach (WallType wt in wallTypes)
            {
                if (wt.Name.Equals("Generic - 8\""))
                {
                    wallType = wt;
                    break;
                }
            }

            Material concrete = new FilteredElementCollector(doc)
                                .OfClass(typeof(Material))
                                .Cast <Material>().FirstOrDefault(q
                                                                  => q.Name == "Concrete, Cast-in-Place gray");

            Material insulation = new FilteredElementCollector(doc)
                                  .OfClass(typeof(Material))
                                  .Cast <Material>().FirstOrDefault(q
                                                                    => q.Name == "EIFS, Exterior Insulation");

            Material lumber = new FilteredElementCollector(doc)
                              .OfClass(typeof(Material))
                              .Cast <Material>().FirstOrDefault(q
                                                                => q.Name == "Softwood, Lumber");

            Material gypsum = new FilteredElementCollector(doc)
                              .OfClass(typeof(Material))
                              .Cast <Material>().FirstOrDefault(q
                                                                => q.Name == "Gypsum Wall Board");

            WallType newWallType = null;

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Basement exterior wall");

                newWallType = wallType.Duplicate("Basement") as WallType;
                // FIXME : this layer should have the "structural material" parameter set
                CompoundStructureLayer l1        = new CompoundStructureLayer(8.0 / 12, MaterialFunctionAssignment.Structure, concrete.Id);
                CompoundStructureLayer newLayer  = new CompoundStructureLayer(3.0 / 12, MaterialFunctionAssignment.Insulation, insulation.Id);
                CompoundStructureLayer newLayer2 = new CompoundStructureLayer(3.5 / 12, MaterialFunctionAssignment.Finish1, lumber.Id);
                CompoundStructureLayer newLayer3 = new CompoundStructureLayer(.5 / 12, MaterialFunctionAssignment.Finish2, gypsum.Id);

                CompoundStructure structure = newWallType.GetCompoundStructure();

                IList <CompoundStructureLayer> layers = structure.GetLayers();

                layers.Add(l1);
                layers.Add(newLayer);
                layers.Add(newLayer2);
                layers.Add(newLayer3);
                structure.SetLayers(layers);

                structure.DeleteLayer(0);

                structure.SetNumberOfShellLayers(ShellLayerType.Interior, 3);

                newWallType.SetCompoundStructure(structure);

                tx.Commit();
            }

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("2x4 + Gypsum wall");

                newWallType = wallType.Duplicate("2x4 + Gypsum") as WallType;
                CompoundStructureLayer l1 = new CompoundStructureLayer(3.5 / 12, MaterialFunctionAssignment.Structure, lumber.Id);
                CompoundStructureLayer l2 = new CompoundStructureLayer(0.5 / 12, MaterialFunctionAssignment.Finish1, gypsum.Id);

                CompoundStructure structure = newWallType.GetCompoundStructure();

                IList <CompoundStructureLayer> layers = structure.GetLayers();

                layers.Add(l2);
                layers.Add(l1);
                layers.Add(l2);
                structure.SetLayers(layers);

                structure.DeleteLayer(0);
                structure.SetNumberOfShellLayers(ShellLayerType.Exterior, 1);
                structure.SetNumberOfShellLayers(ShellLayerType.Interior, 1);

                newWallType.SetCompoundStructure(structure);

                tx.Commit();
            }

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("2x4 + Gypsum wall with Exterior");

                newWallType = wallType.Duplicate("2x4 + Gypsum wall with Exterior") as WallType;
                CompoundStructureLayer l1 = new CompoundStructureLayer(3.5 / 12, MaterialFunctionAssignment.Structure, lumber.Id);
                CompoundStructureLayer l2 = new CompoundStructureLayer(0.5 / 12, MaterialFunctionAssignment.Finish1, gypsum.Id);

                CompoundStructure structure = newWallType.GetCompoundStructure();

                IList <CompoundStructureLayer> layers = structure.GetLayers();

                layers.Add(l2);
                layers.Add(l1);
                layers.Add(l2);
                structure.SetLayers(layers);

                structure.DeleteLayer(0);
                structure.SetNumberOfShellLayers(ShellLayerType.Exterior, 1);
                structure.SetNumberOfShellLayers(ShellLayerType.Interior, 1);


                ElementType wallSweepType = new FilteredElementCollector(doc)
                                            .OfCategory(BuiltInCategory.OST_Cornices)
                                            .WhereElementIsElementType()
                                            .Cast <ElementType>().FirstOrDefault();

                if (wallSweepType != null)
                {
                    var wallSweepInfo = new WallSweepInfo(WallSweepType.Sweep, false);
                    wallSweepInfo.Distance = 2;

                    List <WallSweepInfo> ModSW = new List <WallSweepInfo>();
                    // structure.AddWallSweep(wallSweepInfo);
                }


                newWallType.SetCompoundStructure(structure);

                tx.Commit();
            }

            return(Result.Succeeded);
        }
예제 #5
0
        public static HostObjAttributes ToRevit(this HostPartitionType hostPartitionType, Document document, Core.Revit.ConvertSettings convertSettings)
        {
            if (hostPartitionType == null)
            {
                return(null);
            }

            HostObjAttributes result = null;

            List <HostObjAttributes> hostObjAttributesList = convertSettings?.GetObjects <HostObjAttributes>(hostPartitionType.Guid);

            if (hostObjAttributesList != null)
            {
                if (hostPartitionType is WallType)
                {
                    result = hostObjAttributesList.Find(x => x is Autodesk.Revit.DB.WallType);
                }
                else if (hostPartitionType is FloorType)
                {
                    result = hostObjAttributesList.Find(x => x is Autodesk.Revit.DB.FloorType);
                }
                else if (hostPartitionType is RoofType)
                {
                    result = hostObjAttributesList.Find(x => x is Autodesk.Revit.DB.RoofType);
                }
            }

            if (result != null)
            {
                return(result);
            }

            FilteredElementCollector filteredElementCollector = Query.FilteredElementCollector_New(document, hostPartitionType.GetType())?.OfClass(typeof(HostObjAttributes));

            if (filteredElementCollector == null)
            {
                return(null);
            }

            string familyName_Source = null;
            string typeName_Source   = null;

            if (!Core.Revit.Query.TryGetFamilyNameAndTypeName(hostPartitionType.Name, out familyName_Source, out typeName_Source))
            {
                return(null);
            }

            foreach (HostObjAttributes hostObjAttributes in filteredElementCollector)
            {
                string fullName = Core.Revit.Query.FullName(hostObjAttributes);

                string familyName = null;
                string typeName   = null;
                if (!Core.Revit.Query.TryGetFamilyNameAndTypeName(fullName, out familyName, out typeName))
                {
                    continue;
                }

                if (fullName != null && fullName.Equals(hostPartitionType.Name))
                {
                    result = hostObjAttributes;
                    break;
                }

                if (!string.IsNullOrWhiteSpace(familyName) && !string.IsNullOrWhiteSpace(familyName_Source))
                {
                    continue;
                }


                if (typeName.Equals(typeName_Source))
                {
                    result = hostObjAttributes;
                }
            }

            if (result == null)
            {
                return(result);
            }

            List <Architectural.MaterialLayer> materialLayers = hostPartitionType.MaterialLayers;

            if (materialLayers != null && materialLayers.Count != 0)
            {
                CompoundStructure compoundStructure = result.GetCompoundStructure();
                if (compoundStructure != null)
                {
                    List <Material> materials = new FilteredElementCollector(document).OfClass(typeof(Material)).Cast <Material>().ToList();

                    List <CompoundStructureLayer> compoundStructureLayers = new List <CompoundStructureLayer>();
                    foreach (Architectural.MaterialLayer materialLayer in materialLayers)
                    {
                        Material material = materials.Find(x => x.Name == materialLayer.Name);
                        if (material == null)
                        {
                            continue;
                        }

                        double width = double.NaN;

#if Revit2017 || Revit2018 || Revit2019 || Revit2020
                        width = UnitUtils.ConvertToInternalUnits(materialLayer.Thickness, DisplayUnitType.DUT_METERS);
#else
                        width = UnitUtils.ConvertToInternalUnits(materialLayer.Thickness, UnitTypeId.Meters);
#endif



                        CompoundStructureLayer compoundStructureLayer = new CompoundStructureLayer(width, MaterialFunctionAssignment.Structure, material.Id);
                        if (compoundStructureLayer == null)
                        {
                            continue;
                        }

                        compoundStructureLayers.Add(compoundStructureLayer);
                    }

                    if (compoundStructureLayers != null && compoundStructureLayers.Count != 0)
                    {
                        compoundStructure.SetLayers(compoundStructureLayers);
                        result.SetCompoundStructure(compoundStructure);
                    }
                }
            }

            if (convertSettings.ConvertParameters)
            {
                Core.Revit.Modify.SetValues(result, hostPartitionType);
                Core.Revit.Modify.SetValues(result, hostPartitionType, ActiveSetting.Setting);
            }

            convertSettings?.Add(hostPartitionType.Guid, result);

            return(result);
        }
        public void Execute(UpdaterData updateData)
        {
            try
            {
                var document   = updateData.GetDocument();
                var edits      = updateData.GetModifiedElementIds();
                var collection = CSAContext.GetCollection(document);
                if (CSAContext.IsEditing == true)
                {
                    CSAContext.IsEditing = false;
                    return;
                }
                List <int> movedEntities = new List <int>();
                foreach (var changeId in edits)
                {
                    CSAModel model = null;
                    if (VLConstraintsForCSA.Doc == null)
                    {
                        VLConstraintsForCSA.Doc = document;
                    }

                    #region 根据Target重新生成
                    var targetMoved = collection.Data.FirstOrDefault(c => c.TargetId.IntegerValue == changeId.IntegerValue);
                    if (targetMoved != null)
                    {
                        model = targetMoved;
                        if (movedEntities.Contains(model.TargetId.IntegerValue))
                        {
                            continue;
                        }
                        var creater = CSAContext.Creator;
                        var target  = document.GetElement(model.TargetId);//标注主体失效时删除
                        if (target == null)
                        {
                            collection.Data.Remove(model);
                            continue;
                        }
                        CSAContext.Creator.Regenerate(document, model, target, new XYZ(0, 0, 0));
                        movedEntities.Add(model.TargetId.IntegerValue);
                        CSAContext.IsEditing = true;
                    }
                    #endregion

                    #region 根据Text重新生成
                    var textMoved = collection.Data.FirstOrDefault(c => c.TextNoteIds.FirstOrDefault(p => p.IntegerValue == changeId.IntegerValue) != null);
                    if (textMoved != null)
                    {
                        model = textMoved;
                        if (movedEntities.Contains(model.TargetId.IntegerValue))
                        {
                            continue;
                        }
                        var creater = CSAContext.Creator;
                        var target  = document.GetElement(model.TargetId);//标注主体失效时删除
                        if (target == null)
                        {
                            collection.Data.Remove(model);
                            continue;
                        }
                        //文本更改处理
                        var index   = model.TextNoteIds.IndexOf(changeId);
                        var newText = (document.GetElement(changeId) as TextNote).Text;
                        if (model.Texts[index] != newText)
                        {
                            CompoundStructure compoundStructure = null;
                            HostObjAttributes hoster            = null;
                            if (target is Wall)
                            {
                                hoster            = (HostObjAttributes)((target as Wall).WallType);
                                compoundStructure = hoster.GetCompoundStructure();
                            }
                            if (target is Floor)
                            {
                                hoster            = (HostObjAttributes)((target as Floor).FloorType);
                                compoundStructure = hoster.GetCompoundStructure();
                            }
                            if (target is RoofBase)//屋顶有多种类型
                            {
                                hoster            = (HostObjAttributes)((target as RoofBase).RoofType);
                                compoundStructure = hoster.GetCompoundStructure();
                            }
                            if (compoundStructure == null)
                            {
                                throw new NotImplementedException("关联更新失败,未获取有效的CompoundStructure类型");
                            }
                            var    layers  = compoundStructure.GetLayers();
                            string pattern = @"([\d+\.]+)厚(.+)[\r]?";
                            Regex  regex   = new Regex(pattern);
                            var    match   = regex.Match(newText);
                            if (!match.Success)
                            {
                                PMMessageBox.ShowError("关联更新失败,文本不符合规范");
                                return;
                            }
                            var length       = match.Groups[1].Value;
                            var lengthFoot   = UnitHelper.ConvertToFoot(Convert.ToDouble(length), VLUnitType.millimeter);
                            var materialName = match.Groups[2].Value;
                            var material     = new FilteredElementCollector(document).OfClass(typeof(Material))
                                               .FirstOrDefault(c => c.Name == materialName);
                            if (material == null)
                            {
                                PMMessageBox.ShowError("关联更新失败,未获取到对应名称的材质");
                                return;
                            }
                            //更新
                            layers[index].Width      = lengthFoot;
                            layers[index].MaterialId = material.Id;
                            compoundStructure.SetLayers(layers);
                            IDictionary <int, CompoundStructureError> report = null;
                            IDictionary <int, int> errorMap;
                            try
                            {
                                compoundStructure.IsValid(document, out report, out errorMap);
                                hoster.SetCompoundStructure(compoundStructure);
                            }
                            catch (Exception ex)
                            {
                                PMMessageBox.ShowError("材质设置失败,错误详情:" + (report != null ? string.Join(",", report.Select(c => c.Value)) : ""));
                                throw ex;
                            }

                            ////报错This operation is valid only for non-vertically compound structures
                            //layer = layers[index];
                            //layer.MaterialId = material.Id;
                            ////compoundStructure.SetLayer(index, layer);
                        }
                        else
                        {
                            var textNote = document.GetElement(changeId) as TextNote;
                            if (model.TextNoteTypeElementId.IntegerValue != textNote.TextNoteType.Id.IntegerValue)
                            {
                                model.TextNoteTypeElementId = textNote.TextNoteType.Id;
                                CSAContext.Creator.Regenerate(document, model, target, new XYZ(0, 0, 0));
                            }
                            else
                            {
                                //文本偏移处理
                                //var index = model.TextNoteIds.IndexOf(changeId);
                                //var offset = (document.GetElement(changeId) as TextNote).Coord - model.TextLocations[index];
                                //CompoundStructureAnnotationContext.Creater.Regenerate(document, model, target, offset);
                                //CSAContext.IsEditing = true;//移动会导致偏移 从而二次触发
                            }
                        }
                        movedEntities.Add(model.TargetId.IntegerValue);
                    }
                    #endregion

                    #region 根据Line重新生成
                    var lineMoved = collection.Data.FirstOrDefault(c => c.LineId.IntegerValue == changeId.IntegerValue);
                    if (lineMoved != null)
                    {
                        model = lineMoved;
                        if (movedEntities.Contains(model.TargetId.IntegerValue))
                        {
                            continue;
                        }
                        var creater = CSAContext.Creator;
                        var target  = document.GetElement(model.TargetId);
                        if (target == null)
                        {
                            collection.Data.Remove(model);
                            continue;
                        }
                        CSAContext.Creator.Regenerate(document, model, target);
                        movedEntities.Add(model.TargetId.IntegerValue);
                        CSAContext.IsEditing = true;
                    }
                    #endregion
                }
                CSAContext.Save(document);
            }
            catch (Exception ex)
            {
                VLLogHelper.Error(ex);
            }
        }