본문 바로가기
Revit/Revit API

[레빗 API 시작하기] Revit Intro Lab4 - Element Modification

by Crony 2013. 12. 5.

Revit Intro Lab4 - Element Modification


Revit Intro Lab4 - Element Modification_번역.docx








#region Copyright

//

// Copyright (C) 2010-2013 by Autodesk, Inc.

//

// Permission to use, copy, modify, and distribute this software in

// object code form for any purpose and without fee is hereby granted,

// provided that the above copyright notice appears in all copies and

// that both that copyright notice and the limited warranty and

// restricted rights notice below appear in all supporting

// documentation.

//

// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.

// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF

// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC.

// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE

// UNINTERRUPTED OR ERROR FREE.

//

// Use, duplication, or disclosure by the U.S. Government is subject to

// restrictions set forth in FAR 52.227-19 (Commercial Computer

// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)

// (Rights in Technical Data and Computer Software), as applicable.

//

// Migrated to C# by Adam Nagy 

// 

#endregion // Copyright


#region Namespaces

using System;

using Autodesk.Revit.ApplicationServices;

using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;

using Autodesk.Revit.UI;

using Autodesk.Revit.UI.Selection;

using Util;

#endregion


#region Description

// Revit Intro Lab 4 

// 

// In this lab, you will learn how to modify elements. 

// There are two places to look at when you want to modify an element. 

// (1) at each element level, such as by modifying each properties, parameters and location. 

// (2) use transformation utility methods, such as move, rotate and mirror. 

// 

// for #2, ElementTransformUtils.MoveElement, RotateElement, etc., see pp113 of developer guide. 

// 

// Disclaimer: minimum error checking to focus on the main topic. 

// 

#endregion


namespace IntroCs

{

    /// Element Modification  

    [Transaction(TransactionMode.Automatic)]

    public class ElementModification : IExternalCommand

    {

        // Member 변수 

        Application RvApp;

        Document RvDoc;


        public Result Execute(ExternalCommandData commandData,ref string message,ElementSet elements)

        {

            UIApplication UiApp = commandData.Application;

            UIDocument UiDoc = UiApp.ActiveUIDocument;

            RvApp = UiApp.Application;

            RvDoc = UiDoc.Document;


            // 선택하기 -1

            Reference Ref01 = UiDoc.Selection.PickObject(ObjectType.Element, "Pick a wall, please");

            Element Ent01 = RvDoc.GetElement(Ref01);


            // 요소의 특성, 매개변수, 위치 수정하기

            ModifyElementPropertiesWall(Ent01);

            RvDoc.Regenerate();


            // 선택하기 -2 

            Reference Ref02 = UiDoc.Selection.PickObject(ObjectType.Element, "Pick another element");

            Element Ent02 = RvDoc.GetElement(Ref02);


            // 요소의 이동 및 회전하기 

            ModifyElementByTransformUtilsMethods(Ent02);


            return Result.Succeeded;

        }


        // 서브루틴 : 요소의 특성, 매개변수, 위치 수정하기

        public void ModifyElementPropertiesWall(Element Ent01)

        { 

            // 벽체 예: "Basic Wall: Exterior - Brick on CMU" 

            const string wallFamilyName = Util.Constant.WallFamilyName;

            const string wallTypeName = "Exterior - Brick on CMU";

            const string wallFamilyAndTypeName = wallFamilyName + ": " + wallTypeName;


            if (!(Ent01 is Wall))

            {

                // 선택요소가 벽체가 아닐경우메세지 출력

                TaskDialog.Show(

                  "Modify element properties - wall",

                  "Sorry, I only know how to modify a wall. Please select a wall.");

                return;

            }

            Wall aWall = (Wall)Ent01;


            string msg = "Wall changed:\r\n\r\n";


            // 설정됨 벽체 타입을 newWallType 변수에 저장

            Element newWallType = ElementFiltering.FindFamilyType(RvDoc, typeof(WallType), wallFamilyName, wallTypeName, null);


            if (newWallType != null)

            {

                // newWallType 벽체의 값이 있으면 벽체타입을 변경하고 출력

                aWall.WallType = (WallType)newWallType;

                msg += "Wall type to: " + wallFamilyAndTypeName + "\r\n";

            }


            // Level 1 값을 level1 변수에 저장

            Level level1 = (Level)ElementFiltering.FindElement(RvDoc, typeof(Level), "Level 1", null);

            if (level1 != null)

            {

                // level1 값이 있으면 상단구속 변경하고 출력

                aWall.get_Parameter(BuiltInParameter.WALL_HEIGHT_TYPE).Set(level1.Id);

                msg += "Top Constraint to: Level 1\r\n";

            }


            double topOffset = Constant.MmToFeet(5000.0);

            // Top Offset 5000으로 변경

            aWall.get_Parameter(BuiltInParameter.WALL_TOP_OFFSET).Set(topOffset);


            // Comments "Modified by API" 입력 

            aWall.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS).Set("Modified by API");


            msg += "Top Offset to: 5000.0\r\n";

            msg += "Structural Usage to: Bearing\r\n";

            msg += "Comments added: Modified by API\r\n";


            // 벽체 위치 X: -1,000, Y: -1,000 위치변경

            LocationCurve wallLocation = (LocationCurve)aWall.Location;


            XYZ pt1 = wallLocation.Curve.GetEndPoint(0);

            XYZ pt2 = wallLocation.Curve.GetEndPoint(1);


            double dt = Constant.MmToFeet(1000.0);

            XYZ newPt1 = new XYZ(pt1.X - dt, pt1.Y - dt, pt1.Z);

            XYZ newPt2 = new XYZ(pt2.X - dt, pt2.Y - dt, pt2.Z);


            Line newWallLine = Line.CreateBound(newPt1, newPt2);


            wallLocation.Curve = newWallLine;


            msg += "Location: start point moved -1000.0 in X-direction\r\n";


            // Message to the user. 


            TaskDialog.Show("Modify element properties - wall", msg);

        }


        /// <summary>

        /// A sampler function to demonstrate how to modify an element through its properties. 

        /// Using a door as an example here. 

        /// </summary> 

        public void ModifyElementPropertiesDoor(Element Ent02)

        {

            // Constant to this function. 

            // This is for a door. e.g., "M_Single-Flush: 0762 x 2032mm" 

            // You can modify this to fit your need. 


            const string doorFamilyName = Util.Constant.DoorFamilyName;

            const string doorTypeName = Util.Constant.DoorTypeName2;

            const string doorFamilyAndTypeName = doorFamilyName + ": " + doorTypeName;


            // For simplicity, we assume we can only modify a door 

            if (!(Ent02 is FamilyInstance))

            {

                TaskDialog.Show(

                  "Modify element properties - door",

                  "Sorry, I only know how to modify a door. Please select a door.");

                return;

            }

            FamilyInstance aDoor = (FamilyInstance)Ent02;


            string msg = "Door changed:\n\n";


            // (1) change its family type to a different one. 


            Element newDoorType = ElementFiltering.FindFamilyType(RvDoc, typeof(FamilySymbol), doorFamilyName, doorTypeName, BuiltInCategory.OST_Doors);


            if (newDoorType != null)

            {

                aDoor.Symbol = (FamilySymbol)newDoorType;

                msg += "Door type to: " + doorFamilyAndTypeName + "\r\n";

                //TaskDialog.Show("Modify element properties - door", msg);

            }


            // (2) change its parameters. 

            // leave this as your exercise. 



            // message to the user. 

            TaskDialog.Show("Modify element properties - door", msg);

        }


        /// <summary>

        /// A sampler function that demonstrates how to modify an element 

        /// transform utils methods. 

        /// </summary> 

        public void ModifyElementByTransformUtilsMethods(Element e)

        {

            string msg = "The element changed:\n\n";


            // Try move 

            double dt = Constant.MmToFeet(1000.0);


            // Hard cording for simplicity. 

            XYZ v = new XYZ(dt, dt, 0.0);


            ElementTransformUtils.MoveElement(e.Document, e.Id, v); // 2012


            msg += "move by (1000, 1000, 0)\r\n";


            // Try rotate: 15 degree around z-axis. 

            XYZ pt1 = XYZ.Zero;

            XYZ pt2 = XYZ.BasisZ;

            Line axis = Line.CreateBound(pt1, pt2);


            ElementTransformUtils.RotateElement(e.Document, e.Id, axis, Math.PI / 12.0); // 2012


            msg += "rotate by 15 degree around Z-axis\r\n";


            TaskDialog.Show("Modify element by utils methods", msg);

        }


    }


}