본문 바로가기
Revit/Revit API

[레빗 API 시작하기] Revit Intro Lab7 - Shared Parameter

by Crony 2013. 12. 5.

Revit Intro Lab7 - Shared Parameter


Revit Intro Lab7 - Shared Parameter_번역.docx


이번 Lab에서는 공유매개변수를 생성하는 방법을 배우는 과정입니다.

기존의 프로젝트를 오픈하여서 새로운 클래스를 정의할 새로운 파일을 추가한다.


파일명 :  7_SharedParameter.cs 클래스명 : SharedParameter


추가로 사용될 네임스페이스

System.IO


맴버변수 설정 UiApp UiDoc RvtApp RvtDoc


class SharedParameter : IExternalCommand


1. public static DefinitionFile GetSharedParamsFile(Application app)


2. public static DefinitionGroup GetOrCreateSharedParamsGroup(DefinitionFile sharedParametersFile,string groupName)


3. public static Definition GetOrCreateSharedParamsDefinition(DefinitionGroup defGroup,ParameterType defType,string defName,bool visible)


public class PerDocParameter : IExternalCommand


1. public static Element GetProjectInfoElem(Document doc)



using System;

using System.Diagnostics;

using System.Collections.Generic;

using Autodesk.Revit.ApplicationServices;

using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;

using Autodesk.Revit.UI;

using Autodesk.Revit.UI.Selection;

using Util;

using System.IO;


namespace IntroCs

{

    /// <summary>

    /// Create a new shared parameter, then set and retrieve its value.

    /// In this example, we store a fire rating value on all doors.

    /// Please also look at the FireRating Revit SDK sample.

    /// </summary>

    [Transaction(TransactionMode.Automatic)]

    class SharedParameter : IExternalCommand

    {

        const string kSharedParamsGroupAPI = "API Parameters";

        const string kSharedParamsDefFireRating = "API FireRating";

        const string kSharedParamsPath = "C:\\temp\\SharedParams.txt";


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

        {

            UIDocument UiDoc = commandData.Application.ActiveUIDocument;

            Application RvtApp = commandData.Application.Application;

            Document RvtDoc = UiDoc.Document;


            // Get the current shared params definition file

            DefinitionFile sharedParamsFile = GetSharedParamsFile(RvtApp);

            if (null == sharedParamsFile)

            {

                message = "Error getting the shared params file.";

                return Result.Failed;

            }


            // Get or create the shared params group

            DefinitionGroup sharedParamsGroup = GetOrCreateSharedParamsGroup(

              sharedParamsFile, kSharedParamsGroupAPI);

            if (null == sharedParamsGroup)

            {

                message = "Error getting the shared params group.";

                return Result.Failed;

            }


            Category cat = RvtDoc.Settings.Categories.get_Item(BuiltInCategory.OST_Doors);


            // Visibility of the new parameter:

            // Category.AllowsBoundParameters property indicates if a category can 

            // have shared or project parameters. If it is false, it may not be bound 

            // to shared parameters using the BindingMap. Please note that non-user-visible 

            // parameters can still be bound to these categories. 

            bool visible = cat.AllowsBoundParameters;


            // Get or create the shared params definition

            Definition fireRatingParamDef = GetOrCreateSharedParamsDefinition(

              sharedParamsGroup, ParameterType.Number, kSharedParamsDefFireRating, visible);

            if (null == fireRatingParamDef)

            {

                message = "Error in creating shared parameter.";

                return Result.Failed;

            }


            // Create the category set for binding and add the category

            // we are interested in, doors or walls or whatever:

            CategorySet catSet = RvtApp.Create.NewCategorySet();

            try

            {

                catSet.Insert(cat);

            }

            catch (Exception)

            {

                message = string.Format(

                  "Error adding '{0}' category to parameters binding set.",

                  cat.Name);

                return Result.Failed;

            }


            // Bind the param

            try

            {

                Binding binding = RvtApp.Create.NewInstanceBinding(catSet);

                // We could check if already bound, but looks like Insert will just ignore it in such case

                RvtDoc.ParameterBindings.Insert(fireRatingParamDef, binding);

            }

            catch (Exception ex)

            {

                message = ex.Message;

                return Result.Failed;

            }


            return Result.Succeeded;

        }


        /// <summary>

        /// Helper to get shared parameters file.

        /// </summary>

        public static DefinitionFile GetSharedParamsFile(Application app)

        {

            // Get current shared params file name

            string sharedParamsFileName;

            try

            {

                sharedParamsFileName = app.SharedParametersFilename;

            }

            catch (Exception ex)

            {

                TaskDialog.Show("Get shared params file", "No shared params file set:" + ex.Message);

                return null;

            }


            if (0 == sharedParamsFileName.Length ||

              !System.IO.File.Exists(sharedParamsFileName))

            {

                StreamWriter stream;

                stream = new StreamWriter(kSharedParamsPath);

                stream.Close();

                app.SharedParametersFilename = kSharedParamsPath;

                sharedParamsFileName = app.SharedParametersFilename;

            }


            // Get the current file object and return it

            DefinitionFile sharedParametersFile;

            try

            {

                sharedParametersFile = app.OpenSharedParameterFile();

            }

            catch (Exception ex)

            {

                TaskDialog.Show("Get shared params file", "Cannnot open shared params file:" + ex.Message);

                sharedParametersFile = null;

            }

            return sharedParametersFile;

        }


        public static DefinitionGroup GetOrCreateSharedParamsGroup(

         DefinitionFile sharedParametersFile,

         string groupName)

        {

            DefinitionGroup g = sharedParametersFile.Groups.get_Item(groupName);

            if (null == g)

            {

                try

                {

                    g = sharedParametersFile.Groups.Create(groupName);

                }

                catch (Exception)

                {

                    g = null;

                }

            }

            return g;

        }


        public static Definition GetOrCreateSharedParamsDefinition(

          DefinitionGroup defGroup,

          ParameterType defType,

          string defName,

          bool visible)

        {

            Definition definition = defGroup.Definitions.get_Item(defName);

            if (null == definition)

            {

                try

                {

                    definition = defGroup.Definitions.Create(defName, defType, visible);

                }

                catch (Exception)

                {

                    definition = null;

                }

            }

            return definition;

        }

    }


    [Transaction(TransactionMode.Automatic)]

    public class PerDocParameter : IExternalCommand

    {

        public const string kParamGroupName = "Per-doc Params";

        public const string kParamNameVisible = "Visible per-doc Integer";

        public const string kParamNameInvisible = "Invisible per-doc Integer";


        public Result Execute(

          ExternalCommandData commandData,

          ref string message,

          ElementSet elements)

        {

            UIDocument uiDoc = commandData.Application.ActiveUIDocument;

            Application app = commandData.Application.Application;

            Document doc = uiDoc.Document;


            // get the current shared params definition file

            DefinitionFile sharedParamsFile = SharedParameter.GetSharedParamsFile(app);

            if (null == sharedParamsFile)

            {

                TaskDialog.Show("Per document parameter", "Error getting the shared params file.");

                return Result.Failed;

            }

            // get or create the shared params group

            DefinitionGroup sharedParamsGroup = SharedParameter.GetOrCreateSharedParamsGroup(sharedParamsFile, kParamGroupName);

            if (null == sharedParamsGroup)

            {

                TaskDialog.Show("Per document parameter", "Error getting the shared params group.");

                return Result.Failed;

            }

            // visible param

            Definition docParamDefVisible = SharedParameter.GetOrCreateSharedParamsDefinition(sharedParamsGroup, ParameterType.Integer, kParamNameVisible, true);

            if (null == docParamDefVisible)

            {

                TaskDialog.Show("Per document parameter", "Error creating visible per-doc parameter.");

                return Result.Failed;

            }

            // invisible param

            Definition docParamDefInvisible = SharedParameter.GetOrCreateSharedParamsDefinition(sharedParamsGroup, ParameterType.Integer, kParamNameInvisible, false);

            if (null == docParamDefInvisible)

            {

                TaskDialog.Show("Per document parameter", "Error creating invisible per-doc parameter.");

                return Result.Failed;

            }

            // bind the param

            try

            {

                CategorySet catSet = app.Create.NewCategorySet();

                catSet.Insert(doc.Settings.Categories.get_Item(BuiltInCategory.OST_ProjectInformation));

                Binding binding = app.Create.NewInstanceBinding(catSet);

                doc.ParameterBindings.Insert(docParamDefVisible, binding);

                doc.ParameterBindings.Insert(docParamDefInvisible, binding);

            }

            catch (Exception e)

            {

                TaskDialog.Show("Per document parameter", "Error binding shared parameter: " + e.Message);

                return Result.Failed;

            }

            // set the initial values

            // get the singleton project info element

            Element projInfoElem = GetProjectInfoElem(doc);


            if (null == projInfoElem)

            {

                TaskDialog.Show("Per document parameter", "No project info elem found. Aborting command...");

                return Result.Failed;

            }

            // for simplicity, access params by name rather than by GUID:

            projInfoElem.get_Parameter(kParamNameVisible).Set(55);

            projInfoElem.get_Parameter(kParamNameInvisible).Set(0);

            return Result.Succeeded;

        }


        /// <summary>

        /// Return the one and only project information element using Revit 2009 filtering

        /// by searching for the "Project Information" category. Only one such element exists.

        /// </summary>

        public static Element GetProjectInfoElem(Document doc)

        {

            FilteredElementCollector collector = new FilteredElementCollector(doc);

            collector.OfCategory(BuiltInCategory.OST_ProjectInformation);

            IList<Element> elems = collector.ToElements();


            Debug.Assert(elems.Count == 1, "There should be exactly one of this object in the project");


            return elems[0];

        }

    }

}