コード例 #1
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            // [1] 获取当前文档
            Document doc = commandData.Application.ActiveUIDocument.Document;

            // [2] 获取 CW 102-50-100p 类型的墙
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            Element ele = collector.OfCategory(BuiltInCategory.OST_Walls).OfClass(typeof(WallType)).FirstOrDefault(x => x.Name == "CW 102-50-100P");

            // [3] class transfer
            WallType wallType = ele as WallType;

            // [4] obtain the level
            Level level = new FilteredElementCollector(doc).OfClass(typeof(Level)).FirstOrDefault(x => x.Name == "标高 1") as Level;

            // [5] create control line
            XYZ  start    = new XYZ(0, 0, 0);
            XYZ  end      = new XYZ(10, 10, 0);
            Line geomline = Line.CreateBound(start, end);

            // [6] Assign the height of the wall
            double height = 15 / 0.3048; // inch to meters
            double offset = 0;

            // [7] Using Transaction to Create the wall
            // Command that will edit the document should be included in a transcation
            Transaction trans = new Transaction(doc, "Create the wall");

            trans.Start();
            Wall wall = Wall.Create(doc, geomline, wallType.Id, level.Id, height, offset, false, false);

            trans.Commit();

            return(Result.Succeeded);
        }
コード例 #2
0
        /// <summary>
        /// Создать четыре стены.
        /// </summary>
        /// <returns></returns>
        public List <Wall> CreateWalls()
        {
            // hard coding the size of the house for simplicity
            double width = ElementModification.mmToFeet(10000.0);
            double depth = ElementModification.mmToFeet(5000.0);

            // get the levels we want to work on.
            // Note: hard coding for simplicity. Modify here you use
            // a different template.
            Level level1 = (Level)ElementFiltering.FindElement(m_rvtDoc, typeof(Level), "Level 1", null);

            if (level1 == null)
            {
                TaskDialog.Show("Revit Intro Lab", "Cannot find (Level 1). Maybe you use a different template? Try with DefaultMetric.rte.");
                return(null);
            }

            Level level2 = (Level)ElementFiltering.FindElement(m_rvtDoc, typeof(Level), "Level 2", null);

            if (level2 == null)
            {
                TaskDialog.Show("Revit Intro Lab", "Cannot find (Level 2). Maybe you use a different template? Try with DefaultMetric.rte.");
                return(null);
            }

            // set four corner of walls.
            // 5th point is for combenience to loop through.
            double dx = width / 2.0;
            double dy = depth / 2.0;

            List <XYZ> pts = new List <XYZ>()
            {
                new XYZ(-dx, -dy, 0.0),
                new XYZ(dx, -dy, 0.0),
                new XYZ(dx, dy, 0.0),
                new XYZ(-dx, dy, 0.0)
            };

            pts.Add(pts[0]);

            // flag for structural wall or not.
            bool isStructural = false;

            // save walls we create.
            List <Wall> walls = new List <Wall>(4);

            // loop through list of points and define four walls.
            for (int i = 0; i <= 3; i++)
            {
                // define a base curve from two points.
                Line baseCurve = Line.CreateBound(pts[i], pts[i + 1]);

                // create a wall using the one of overloaded methods.
                Wall aWall = Wall.Create(m_rvtDoc, baseCurve, level1.Id, isStructural);

                // set the Top Constraint to Level 2
                aWall.get_Parameter(BuiltInParameter.WALL_HEIGHT_TYPE).Set(level2.Id);

                // save the wall.
                walls.Add(aWall);
            }

            // This is important. we need these lines to have shrinkwrap working.
            m_rvtDoc.Regenerate();
            m_rvtDoc.AutoJoinElements();

            return(walls);
        }