Пример #1
0
        public static void ExportParameters(Document doc, string path)
        {
            DefinitionFile existingDefFile = doc.Application.OpenSharedParameterFile();

            //
            doc.Application.SharedParametersFilename = path;
            DefinitionFile tempDefFile = doc.Application.OpenSharedParameterFile();

            // Manager the Exported group
            DefinitionGroup exported = null;

            var query = from dg in tempDefFile.Groups
                        where dg.Name.Equals(doc.PathName + "-Exported")
                        select dg;

            if (query.Count() == 0)
            {
                exported = tempDefFile.Groups.Create(doc.PathName + "-Exported");
            }
            else
            {
                exported = query.First();
            }

            // Iterate over the shared parameters in the document
            BindingMap bindingMap           = doc.ParameterBindings;
            DefinitionBindingMapIterator it =
                bindingMap.ForwardIterator();

            it.Reset();

            while (it.MoveNext())
            {
                InternalDefinition        definition  = it.Key as InternalDefinition;
                Autodesk.Revit.DB.Binding currBinding = bindingMap.get_Item(definition);

                // Corroborate that the current parameter has not been exported previously
                Definition existingDef = exported.Definitions
                                         .Where(d => (d.Name.Equals(definition.Name))).FirstOrDefault();
                if (existingDef != null)
                {
                    continue;
                }

                // unearth and assign the parameter's GUID
                SharedParameterElement sharedParamElem =
                    doc.GetElement(definition.Id) as SharedParameterElement;

                if (sharedParamElem == null)
                {
                    continue;
                }

                ExternalDefinitionCreationOptions options =
                    new ExternalDefinitionCreationOptions(definition.Name, definition.ParameterType);
                options.GUID = sharedParamElem.GuidValue;

                Definition createdDefinition = exported.Definitions.Create(options);
            }
        }
Пример #2
0
        /// <summary>
        /// Returns a list of the objects containing
        /// references to the project parameter definitions
        /// </summary>
        /// <param name="doc">The project document being quereied</param>
        /// <returns></returns>
        static List <ProjectParameterData> GetProjectParameterData(Document doc)
        {
            // Following good SOA practices, first validate incoming parameters

            if (doc == null)
            {
                throw new ArgumentNullException("doc");
            }

            if (doc.IsFamilyDocument)
            {
                throw new Exception("doc can not be a family document.");
            }

            List <ProjectParameterData> result = new List <ProjectParameterData>();

            BindingMap map = doc.ParameterBindings;
            DefinitionBindingMapIterator it
                = map.ForwardIterator();

            it.Reset();
            while (it.MoveNext())
            {
                ProjectParameterData newProjectParameterData = new ProjectParameterData();

                newProjectParameterData.Definition = it.Key;
                newProjectParameterData.Name       = it.Key.Name;
                newProjectParameterData.Binding    = it.Current
                                                     as ElementBinding;

                result.Add(newProjectParameterData);
            }
            return(result);
        }
Пример #3
0
        public static IList <SharedParameter> GetSharedParameters(Document doc)
        {
            IList <SharedParameter> extSharedParams = new List <SharedParameter>();
            BindingMap bindingMap           = doc.ParameterBindings;
            DefinitionBindingMapIterator it =
                bindingMap.ForwardIterator();

            it.Reset();

            while (it.MoveNext())
            {
                InternalDefinition        definition  = it.Key as InternalDefinition;
                Autodesk.Revit.DB.Binding currBinding = bindingMap.get_Item(definition);

                // unearth the parameter's GUID
                SharedParameterElement sharedParamElem =
                    doc.GetElement(definition.Id) as SharedParameterElement;

                if (sharedParamElem == null)
                {
                    continue;
                }

                SharedParameter sharedParam =
                    new SharedParameter(definition.Name, definition.ParameterType,
                                        sharedParamElem.GuidValue, currBinding, definition.ParameterGroup);

                extSharedParams.Add(sharedParam);
            }

            return(extSharedParams);
        }
Пример #4
0
        /// <summary>
        /// Has the specific document shared parameter already been added ago?
        /// </summary>
        /// <param name="doc">Revit project in which the shared parameter will be added.</param>
        /// <param name="paraName">the name of the shared parameter.</param>
        /// <param name="boundCategory">Which category the parameter will bind to</param>
        /// <returns>Returns true if already added ago else returns false.</returns>
        private static bool AlreadyAddedSharedParameter(Document doc, string paraName, BuiltInCategory boundCategory)
        {
            try
            {
                BindingMap bindingMap = doc.ParameterBindings;
                DefinitionBindingMapIterator bindingMapIter = bindingMap.ForwardIterator();

                while (bindingMapIter.MoveNext())
                {
                    if (bindingMapIter.Key.Name.Equals(paraName))
                    {
                        ElementBinding binding    = bindingMapIter.Current as ElementBinding;
                        CategorySet    categories = binding.Categories;

                        foreach (Category category in categories)
                        {
                            if (category.Id.IntegerValue.Equals((int)boundCategory))
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                return(false);
            }

            return(false);
        }
Пример #5
0
        //Method deletes parameters
        public void RemoveSharedParameterBinding(Application app, string name, ParameterType type)
        {
            BindingMap map = (new UIApplication(app)).ActiveUIDocument.Document.ParameterBindings;
            DefinitionBindingMapIterator it = map.ForwardIterator();

            it.Reset();

            Definition def = null;

            while (it.MoveNext())
            {
                if (it.Key != null && it.Key.Name == name && type == it.Key.ParameterType)
                {
                    def = it.Key;
                    break;
                }
            }

            if (def == null)
            {
                sbFeedback.Append("Parameter " + name + " does not exist.\n");
            }
            else
            {
                map.Remove(def);
                if (map.Contains(def))
                {
                    sbFeedback.Append("Failed to delete parameter " + name + " for some reason.\n");
                }
                else
                {
                    sbFeedback.Append("Parameter " + name + " deleted.\n");
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Test if the Room binds a specified shared parameter
        /// </summary>
        /// <param name="paramName">Parameter name to be checked</param>
        /// <returns>true, the definition exists, false, doesn't exist.</returns>
        private bool ShareParameterExists(String paramName)
        {
            BindingMap bindingMap             = m_document.ParameterBindings;
            DefinitionBindingMapIterator iter = bindingMap.ForwardIterator();

            iter.Reset();

            while (iter.MoveNext())
            {
                Definition tempDefinition = iter.Key;

                // find the definition of which the name is the appointed one
                if (String.Compare(tempDefinition.Name, paramName) != 0)
                {
                    continue;
                }

                // get the category which is bound
                ElementBinding binding        = bindingMap.get_Item(tempDefinition) as ElementBinding;
                CategorySet    bindCategories = binding.Categories;
                foreach (Category category in bindCategories)
                {
                    if (category.Name
                        == m_document.Settings.Categories.get_Item(BuiltInCategory.OST_Rooms).Name)
                    {
                        // the definition with appointed name was bound to Rooms, return true
                        return(true);
                    }
                }
            }
            //
            // return false if shared parameter doesn't exist
            return(false);
        }
        /// <summary>
        /// Checks if a parameter exists based of a name
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="paramName"></param>
        /// <returns></returns>
        private bool ShareParameterExists(Document doc, String paramName)
        {
            BindingMap bindingMap             = doc.ParameterBindings;
            DefinitionBindingMapIterator iter = bindingMap.ForwardIterator();

            iter.Reset();

            while (iter.MoveNext())
            {
                Definition tempDefinition = iter.Key;

                // find the definition of which the name is the appointed one
                if (String.Compare(tempDefinition.Name, paramName) != 0)
                {
                    continue;
                }

                // get the category which is bound
                ElementBinding binding        = bindingMap.get_Item(tempDefinition) as ElementBinding;
                CategorySet    bindCategories = binding.Categories;
                foreach (Category category in bindCategories)
                {
                    if (category.Name
                        == doc.Settings.Categories.get_Item(BuiltInCategory.OST_Rebar).Name)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Пример #8
0
        private Dictionary <string, Dictionary <string, ParameterProperties> > GetProjectParameters()
        {
            Dictionary <string, Dictionary <string, ParameterProperties> > paramDictionary = new Dictionary <string, Dictionary <string, ParameterProperties> >();

            try
            {
                BindingMap bindingMap = m_doc.ParameterBindings;
                DefinitionBindingMapIterator iterator = bindingMap.ForwardIterator();
                while (iterator.MoveNext())
                {
                    Definition     definition = iterator.Key as Definition;
                    string         paramName  = definition.Name;
                    ElementBinding binding    = iterator.Current as ElementBinding;

                    ParameterProperties pp = new ParameterProperties();
                    pp.ParameterName = paramName;
                    if (binding is InstanceBinding)
                    {
                        pp.IsInstance = true;
                    }
                    else if (binding is TypeBinding)
                    {
                        pp.IsInstance = false;
                    }
                    foreach (Category category in binding.Categories)
                    {
                        if (!string.IsNullOrEmpty(category.Name))
                        {
                            if (!paramDictionary.ContainsKey(category.Name))
                            {
                                Dictionary <string, ParameterProperties> dictionary = new Dictionary <string, ParameterProperties>();
                                dictionary.Add(pp.ParameterName, pp);
                                paramDictionary.Add(category.Name, dictionary);
                            }
                            else
                            {
                                if (!paramDictionary[category.Name].ContainsKey(pp.ParameterName))
                                {
                                    paramDictionary[category.Name].Add(pp.ParameterName, pp);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to get project parameters\n" + ex.Message, "Get Project Parameters", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(paramDictionary);
        }
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication app   = commandData.Application;
            UIDocument    uidoc = app.ActiveUIDocument;
            Document      doc   = uidoc.Document;

            BindingMap bindings = doc.ParameterBindings;

            int n = bindings.Size;

            Debug.Print("{0} shared parementer{1} defined{2}",
                        n, Util.PluralSuffix(n), Util.DotOrColon(n));

            if (0 < n)
            {
                DefinitionBindingMapIterator it
                    = bindings.ForwardIterator();

                while (it.MoveNext())
                {
                    Definition d = it.Key as Definition;
                    Binding    b = it.Current as Binding;

                    Debug.Assert(b is ElementBinding,
                                 "all Binding instances are ElementBinding instances");

                    Debug.Assert(b is InstanceBinding ||
                                 b is TypeBinding,
                                 "all bindings are either instance or type");

                    // All definitions obtained in this manner
                    // are InternalDefinition instances, even
                    // if they are actually associated with
                    // shared parameters, i.e. external.

                    Debug.Assert(d is InternalDefinition,
                                 "all definitions obtained from BindingMap are internal");

                    string sbinding = (b is InstanceBinding)
          ? "instance"
          : "type";

                    Debug.Print("{0}: {1}", d.Name, sbinding);
                }
            }
            return(Result.Succeeded);
        }
Пример #10
0
        static public bool IsParameterInProject(Document doc, string parameterName, BuiltInCategory cat)
        {
            Category   category             = doc.Settings.Categories.get_Item(cat);
            BindingMap map                  = doc.ParameterBindings;
            DefinitionBindingMapIterator it = map.ForwardIterator();
            bool result = false;

            it.Reset();
            while (it.MoveNext())
            {
                result = result || (it.Key.Name == parameterName && (it.Current as ElementBinding).Categories.Contains(category));
            }
            return(result);
        }
Пример #11
0
        bool DoesExist(Document doc, string paramName)
        {
            DefinitionBindingMapIterator itr =
                doc.ParameterBindings.ForwardIterator();

            while (itr.MoveNext())
            {
                if (itr.Key.Name == paramName)
                {
                    return(true);
                }
            }
            return(false);
        }
Пример #12
0
        //Shared Parameters
        //-----------------
        public static bool CheckCreateProjectParameter(Document doc, string param, BuiltInCategory bicCheck)
        {
            try
            {
                BindingMap map = doc.ParameterBindings;

                DefinitionBindingMapIterator it = map.ForwardIterator();
                it.Reset();

                Category cat = Category.GetCategory(doc, bicCheck);

                CategorySet catSet = new CategorySet();
                catSet.Insert(Category.GetCategory(doc, bicCheck));

                ElementBinding eb = null;

                while (it.MoveNext())
                {
                    if (param == it.Key.Name)
                    {
                        eb = it.Current as ElementBinding;
                        if (eb.Categories.Contains(cat))
                        {
                            return(true);
                        }
                    }
                }

                Transaction trans = new Transaction(doc);

                try
                {
                    trans.Start("Project Parameters");
                    CreateSharedProjectParameters(doc, param, catSet);
                    trans.Commit();

                    return(true);
                }
                catch (Exception ex)
                {
                    trans.RollBack();
                    throw new Exception("Exception in Create Shared Parameters" + ex.Message);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Exception in Check Project Parameter.\n" + ex);
            }
        }
Пример #13
0
        static private ElementBinding GetParameterBinding(Document doc, string parameterName)
        {
            BindingMap map = doc.ParameterBindings;
            DefinitionBindingMapIterator it = map.ForwardIterator();

            it.Reset();
            while (it.MoveNext())
            {
                if (it.Key.Name == parameterName)
                {
                    return(it.Current as ElementBinding);
                }
            }
            return(null);
        }
Пример #14
0
        protected internal static Definition GetParameterDefinition(Document doc, string name)
        {
            BindingMap val = doc.get_ParameterBindings();
            DefinitionBindingMapIterator val2 = val.ForwardIterator();

            val2.Reset();
            while (val2.MoveNext())
            {
                InternalDefinition val3 = val2.get_Key();
                if (val3.get_Name().Equals(name))
                {
                    return(val3);
                }
            }
            return(null);
        }
Пример #15
0
        public static bool TryGetElementBinding(this Document document, Guid guid, out InternalDefinition internalDefinition, out ElementBinding elementBinding)
        {
            internalDefinition = null;
            elementBinding     = null;

            if (document == null || document.ParameterBindings == null)
            {
                return(false);
            }

            DefinitionBindingMapIterator definitionBindingMapIterator = document.ParameterBindings.ForwardIterator();

            if (definitionBindingMapIterator == null)
            {
                return(false);
            }

            while (definitionBindingMapIterator.MoveNext())
            {
                InternalDefinition internalDefinition_Temp = definitionBindingMapIterator.Key as InternalDefinition;
                if (internalDefinition_Temp == null)
                {
                    continue;
                }

                ElementId elementId = internalDefinition_Temp.Id;
                if (elementId == null || elementId == Autodesk.Revit.DB.ElementId.InvalidElementId)
                {
                    continue;
                }

                SharedParameterElement sharedParameterElement = document.GetElement(elementId) as SharedParameterElement;
                if (sharedParameterElement == null)
                {
                    continue;
                }

                if (guid == sharedParameterElement.GuidValue)
                {
                    elementBinding     = (ElementBinding)definitionBindingMapIterator.Current;
                    internalDefinition = internalDefinition_Temp;
                    return(true);
                }
            }
            return(false);
        }
Пример #16
0
        public static bool TryGetElementBinding(this Document document, string name, out InternalDefinition internalDefinition, out ElementBinding elementBinding)
        {
            internalDefinition = null;
            elementBinding     = null;

            if (document == null || document.ParameterBindings == null || string.IsNullOrEmpty(name))
            {
                return(false);
            }

            DefinitionBindingMapIterator definitionBindingMapIterator = document.ParameterBindings.ForwardIterator();

            if (definitionBindingMapIterator == null)
            {
                return(false);
            }

            while (definitionBindingMapIterator.MoveNext())
            {
                InternalDefinition aInternalDefinition_Temp = definitionBindingMapIterator.Key as InternalDefinition;
                if (aInternalDefinition_Temp == null)
                {
                    continue;
                }

                ElementId elementId = aInternalDefinition_Temp.Id;
                if (elementId == null || elementId == Autodesk.Revit.DB.ElementId.InvalidElementId)
                {
                    continue;
                }

                ParameterElement parameterElement = document.GetElement(elementId) as ParameterElement;
                if (parameterElement == null)
                {
                    continue;
                }

                if (name == parameterElement.Name)
                {
                    elementBinding     = (ElementBinding)definitionBindingMapIterator.Current;
                    internalDefinition = aInternalDefinition_Temp;
                    return(true);
                }
            }
            return(false);
        }
Пример #17
0
        static List <RoomProjectParametersData> GetRoomProjectParametersData(Document doc)
        {
            // Following good SOA practices, first validate incoming parameters
            if (doc == null)
            {
                throw new ArgumentNullException("doc");
            }

            if (doc.IsFamilyDocument)
            {
                throw new Exception("This plugin cannot be run on a family document.");
            }

            List <RoomProjectParametersData> result
                = new List <RoomProjectParametersData>();

            BindingMap map = doc.ParameterBindings;
            DefinitionBindingMapIterator it
                = map.ForwardIterator();

            it.Reset();
            while (it.MoveNext())
            {
                RoomProjectParametersData newProjectParameterData
                    = new RoomProjectParametersData();

                newProjectParameterData.Binding = it.Current
                                                  as ElementBinding;

                Category roomCategory = Category.GetCategory(doc, BuiltInCategory.OST_Rooms);

                IEnumerable <Category> catSet = newProjectParameterData.Binding.Categories.Cast <Category>();

                if (catSet.Any(c => c.Id.Equals(roomCategory.Id)) && it.Key.ParameterType == ParameterType.Text)
                {
                    newProjectParameterData.category   = roomCategory;
                    newProjectParameterData.Definition = it.Key;
                    newProjectParameterData.Name       = it.Key.Name;
                    newProjectParameterData.type       = it.Key.ParameterType;

                    result.Add(newProjectParameterData);
                }
            }
            return(result);
        }
Пример #18
0
        internal bool DoesParameterExist(string parameterName)
        {
            // iterate the procedure over
            // all the shared parameters in the document
            BindingMap bindingMap            = m_doc.ParameterBindings;
            DefinitionBindingMapIterator itr = bindingMap.ForwardIterator();

            while (itr.MoveNext())
            {
                InternalDefinition internalDefinition
                    = itr.Key as InternalDefinition;
                if (internalDefinition.Name == parameterName)
                {
                    return(true);
                }
            }
            return(false);
        }
        private ElementBinding GetBindingByParamName(String paramName, Document doc)
        {
            Application app = doc.Application;
            DefinitionBindingMapIterator iter = doc.ParameterBindings.ForwardIterator();

            while (iter.MoveNext())
            {
                Definition curDef = iter.Key;
                if (!Name.Equals(curDef.Name))
                {
                    continue;
                }

                def = curDef;
                ElementBinding elemBind = (ElementBinding)iter.Current;
                return(elemBind);
            }
            throw new Exception("не найден параметр " + paramName);
        }
Пример #20
0
        public static IEnumerable <DefinitionInfo> IterateParameters(Document doc, Predicate <DefinitionBindingMapIterator> predicate)
        {
            var bindings = doc.ParameterBindings;
            int n        = bindings.Size;

            if (0 < n)
            {
                DefinitionBindingMapIterator it = bindings.ForwardIterator();
                while (it.MoveNext())
                {
                    Debug.WriteLine($"{it.Key.Name}, {it.Key}, {it.Key.ParameterType}");
                    if (predicate(it))
                    {
                        var categories = (it.Current as ElementBinding)?.Categories.OfType <Category>().
                                         Select(s => (BuiltInCategory)s.Id.IntegerValue).ToList();
                        yield return(new DefinitionInfo(it.Key, categories));
                    }
                }
            }
        }
Пример #21
0
        public static bool IsAlreadyBound(Autodesk.Revit.DB.Document doc, string paramName)
        {
            DefinitionBindingMapIterator iter
                = doc.ParameterBindings.ForwardIterator();

            while (iter.MoveNext())
            {
                Definition     def = iter.Key;
                ElementBinding elemBind
                    = (ElementBinding)iter.Current;

                // Got param name match

                if (paramName.Equals(def.Name,
                                     StringComparison.CurrentCultureIgnoreCase))
                {
                    return(true);
                }
            }
            return(false);
        }
Пример #22
0
        internal void CanVaryBtwGroups(
            string parameterName,
            bool allowVaryBtwGrps)
        {
            BindingMap bindingMap            = m_doc.ParameterBindings;
            DefinitionBindingMapIterator itr = bindingMap.ForwardIterator();

            while (itr.MoveNext())
            {
                InternalDefinition internalDef = itr.Key as InternalDefinition;

                if (internalDef.Name == parameterName)
                {
                    using (Transaction t = new Transaction(m_doc)) {
                        t.Start("Allow varying b/w groups");
                        internalDef.SetAllowVaryBetweenGroups(m_doc, allowVaryBtwGrps);
                        t.Commit();
                    }
                }
            }
        }
Пример #23
0
        private static void CreateProjectParam(Autodesk.Revit.ApplicationServices.Application app,
                                               string name, ParameterType type, bool visible, CategorySet catSet, BuiltInParameterGroup group, bool inst)
        {
            // Check whether the parameter has already been added.
            BindingMap map = (new UIApplication(app)).ActiveUIDocument.Document.ParameterBindings;
            DefinitionBindingMapIterator itr = map.ForwardIterator();

            while (itr.MoveNext())
            {
                Definition exstDef = itr.Key;
                if (exstDef.Name == "LinkedTag")
                {
                    return;
                }
            }

            string orgFile  = app.SharedParametersFilename;
            string tempFile = Path.GetTempFileName() + ".txt";

            using (File.Create(tempFile)) { }
            app.SharedParametersFilename = tempFile;

            ExternalDefinition def = app.OpenSharedParameterFile().Groups.Create("TemporaryDefinitionGroup").Definitions.Create
                                         (new ExternalDefinitionCreationOptions(name, type)
            {
                Visible = visible
            }) as ExternalDefinition;

            app.SharedParametersFilename = orgFile;
            File.Delete(tempFile);

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

            if (inst)
            {
                binding = app.Create.NewInstanceBinding(catSet);
            }

            map.Insert(def, binding);
        }
Пример #24
0
        public static List <RawProjectParameterInfo> RawGetProjectParametersInfo(Document doc)
        {
            RawProjectParameterInfo.FileName = doc.Title;
            List <RawProjectParameterInfo> paramList = new List <RawProjectParameterInfo>();

            BindingMap map = doc.ParameterBindings;
            DefinitionBindingMapIterator it = map.ForwardIterator();

            it.Reset();
            while (it.MoveNext())
            {
                ElementBinding  eleBinding = it.Current as ElementBinding;
                InstanceBinding insBinding = eleBinding as InstanceBinding;
                Definition      def        = it.Key;
                if (def != null)
                {
                    ExternalDefinition extDef = def as ExternalDefinition;
                    bool shared = extDef != null;
                    RawProjectParameterInfo param = new RawProjectParameterInfo
                    {
                        Name            = def.Name,
                        Group           = def.ParameterGroup,
                        Type            = def.ParameterType,
                        ReadOnly        = true, // def.IsReadOnly, def.IsReadOnly NOT working in either 2015 or 2014 but working in 2013
                        BoundToInstance = insBinding != null,
                        BoundCategories = RawConvertSetToList <Category>(eleBinding.Categories).Select(c => c.Name).ToArray(),

                        FromShared = shared,
                        GUID       = shared ? extDef.GUID.ToString() : string.Empty,
                        Owner      = shared ? extDef.OwnerGroup.Name : string.Empty,
                        Visible    = shared ? extDef.Visible : true,
                    };

                    paramList.Add(param);
                }
            }

            return(paramList);
        }
Пример #25
0
        public ParameterListForm(UIApplication revitUiApp, DataSet dataSet) : this()
        {
            this.m_revitUiApp                   = revitUiApp;
            this.m_revitApp                     = revitUiApp.Application;
            this.m_tableInfoSet                 = Command.TableInfoSet;
            this.m_parameterCreation            = new ParameterCreation(revitUiApp);
            this.m_dataSet                      = dataSet;
            this.parameterListBox.DisplayMember = "ParameterName";
            this.parameterListBox.Sorted        = true;
            DefinitionBindingMapIterator definitionBindingMapIterator = this.m_revitUiApp.ActiveUIDocument.Document.ParameterBindings.ForwardIterator();

            definitionBindingMapIterator.Reset();
            while (definitionBindingMapIterator.MoveNext())
            {
                Definition key  = definitionBindingMapIterator.Key;
                string     name = key.Name;
                if (!this.ParameterExists(name))
                {
                    this.parameterListBox.Items.Add(name);
                }
            }
        }
Пример #26
0
        public static Dictionary <string /*paramName*/, List <string> /*categoryNames*/> GetProjectParameterInfo(Document doc)
        {
            Dictionary <string, List <string> > projectParameters = new Dictionary <string, List <string> >();

            try
            {
                BindingMap bindingMap = doc.ParameterBindings;
                DefinitionBindingMapIterator iterator = bindingMap.ForwardIterator();

                while (iterator.MoveNext())
                {
                    Definition     definition     = iterator.Key as Definition;
                    string         paramName      = definition.Name;
                    ElementBinding elementBinding = iterator.Current as ElementBinding;
                    List <string>  categoryNames  = new List <string>();
                    if (null != elementBinding)
                    {
                        foreach (Category category in elementBinding.Categories)
                        {
                            if (!string.IsNullOrEmpty(category.Name))
                            {
                                categoryNames.Add(category.Name);
                            }
                        }
                    }

                    if (!projectParameters.ContainsKey(paramName))
                    {
                        projectParameters.Add(paramName, categoryNames);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to get project parameter information.\n" + ex.Message, "Get Project Parameter Info", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(projectParameters);
        }
Пример #27
0
        /// <summary>
        /// Method to retrieve filtered project parameters ID by category
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="category"></param>
        /// <returns></returns>
        public static string[] ProjectParameters(Document doc, string categoryName)
        {
            List <string> parametersID = new List <string>();

            BindingMap map = doc.ParameterBindings;
            DefinitionBindingMapIterator it = map.ForwardIterator();

            it.Reset();

            while (it.MoveNext())
            {
                ElementBinding  eleBinding = it.Current as ElementBinding;
                InstanceBinding insBinding = eleBinding as InstanceBinding;

                if (insBinding != null && IsInstBindingOfCategory(insBinding, categoryName))
                {
                    Definition def = it.Key;
                    if (def != null)
                    {
                        ExternalDefinition extDef = def as ExternalDefinition;

                        if (extDef != null)
                        {
                            string GUID = extDef.GUID.ToString();
                            parametersID.Add(GUID);
                        }
                        else
                        {
                            InternalDefinition intDef = def as InternalDefinition;
                            string             ID     = intDef.Id.ToString();
                            parametersID.Add(ID);
                        }
                    }
                }
            }
            return(parametersID.ToArray());
        }
Пример #28
0
    {//----------------------------------------------------------
        public static void get_share_parameter(Document doc, ObservableCollection <data_parameter> my_data_parameter_need, ObservableCollection <data_parameter> my_data_parameter_current)
        {
            try
            {
                if (File.Exists(Source.path_share_parameter_default))
                {
                    List <string> du_lieu           = File.ReadAllLines(Source.path_share_parameter_default).ToList();
                    BindingMap    map               = doc.ParameterBindings;
                    DefinitionBindingMapIterator it = map.ForwardIterator();
                    while (it.MoveNext())
                    {
                        Definition def = it.Key;
                        my_data_parameter_current.Add(new data_parameter()
                        {
                            ten_parameter = def.Name,
                            color         = du_lieu.Any(x => x == def.Name) == false ? Source.color_error : Source.color
                        });
                    }



                    foreach (string data in du_lieu)
                    {
                        my_data_parameter_need.Add(new data_parameter()
                        {
                            ten_parameter = data,
                            color         = my_data_parameter_current.Any(x => x.ten_parameter == data) ? Source.color : Source.color_error
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public static BindSharedParamResult BindSharedParam(
            Document doc,
            Category cat,
            string paramName,
            string grpName,
            ParameterType paramType,
            bool visible,
            bool instanceBinding)
        {
            try // generic
            {
                Application app = doc.Application;

                // This is needed already here to
                // store old ones for re-inserting

                CategorySet catSet = app.Create.NewCategorySet();

                // Loop all Binding Definitions
                // IMPORTANT NOTE: Categories.Size is ALWAYS 1 !?
                // For multiple categories, there is really one
                // pair per each category, even though the
                // Definitions are the same...

                DefinitionBindingMapIterator iter
                    = doc.ParameterBindings.ForwardIterator();

                while (iter.MoveNext())
                {
                    Definition     def = iter.Key;
                    ElementBinding elemBind
                        = (ElementBinding)iter.Current;

                    // Got param name match

                    if (paramName.Equals(def.Name,
                                         StringComparison.CurrentCultureIgnoreCase))
                    {
                        // Check for category match - Size is always 1!

                        if (elemBind.Categories.Contains(cat))
                        {
                            // Check Param Type

                            if (paramType != def.ParameterType)
                            {
                                return(BindSharedParamResult.eWrongParamType);
                            }

                            // Check Binding Type

                            if (instanceBinding)
                            {
                                if (elemBind.GetType() != typeof(InstanceBinding))
                                {
                                    return(BindSharedParamResult.eWrongBindingType);
                                }
                            }
                            else
                            {
                                if (elemBind.GetType() != typeof(TypeBinding))
                                {
                                    return(BindSharedParamResult.eWrongBindingType);
                                }
                            }

                            // Check Visibility - cannot (not exposed)
                            // If here, everything is fine,
                            // ie already defined correctly

                            return(BindSharedParamResult.eAlreadyBound);
                        }

                        // If here, no category match, hence must
                        // store "other" cats for re-inserting

                        else
                        {
                            foreach (Category catOld
                                     in elemBind.Categories)
                            {
                                catSet.Insert(catOld); // 1 only, but no index...
                            }
                        }
                    }
                }

                // If here, there is no Binding Definition for
                // it, so make sure Param defined and then bind it!

                DefinitionFile defFile
                    = GetOrCreateSharedParamsFile(app);

                DefinitionGroup defGrp
                    = GetOrCreateSharedParamsGroup(
                          defFile, grpName);

                Definition definition
                    = GetOrCreateSharedParamDefinition(
                          defGrp, paramType, paramName, visible);

                catSet.Insert(cat);

                InstanceBinding bind = null;

                if (instanceBinding)
                {
                    bind = app.Create.NewInstanceBinding(
                        catSet);
                }
                else
                {
                    bind = app.Create.NewTypeBinding(catSet);
                }

                // There is another strange API "feature".
                // If param has EVER been bound in a project
                // (in above iter pairs or even if not there
                // but once deleted), Insert always fails!?
                // Must use .ReInsert in that case.
                // See also similar findings on this topic in:
                // http://thebuildingcoder.typepad.com/blog/2009/09/adding-a-category-to-a-parameter-binding.html
                // - the code-idiom below may be more generic:

                if (doc.ParameterBindings.Insert(
                        definition, bind))
                {
                    return(BindSharedParamResult.eSuccessfullyBound);
                }
                else
                {
                    if (doc.ParameterBindings.ReInsert(
                            definition, bind))
                    {
                        return(BindSharedParamResult.eSuccessfullyBound);
                    }
                    else
                    {
                        return(BindSharedParamResult.eFailed);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format(
                                    "Error in Binding Shared Param: {0}",
                                    ex.Message));

                return(BindSharedParamResult.eFailed);
            }
        }
        public Result ExecuteObsolete(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication app   = commandData.Application;
            UIDocument    uidoc = app.ActiveUIDocument;
            Document      doc   = uidoc.Document;

            BindingMap bindings = doc.ParameterBindings;
            //Dictionary<string, Guid> guids = new Dictionary<string, Guid>();
            Dictionary <Definition, object> mapDefToGuid = new Dictionary <Definition, object>();

            int n = bindings.Size;

            Debug.Print("{0} shared parementer{1} defined{2}",
                        n, Util.PluralSuffix(n), Util.DotOrColon(n));

            if (0 < n)
            {
                DefinitionBindingMapIterator it
                    = bindings.ForwardIterator();

                while (it.MoveNext())
                {
                    Definition d = it.Key as Definition;
                    Binding    b = it.Current as Binding;
                    if (d is ExternalDefinition)
                    {
                        Guid g = ((ExternalDefinition)d).GUID;
                        Debug.Print(d.Name + ": " + g.ToString());
                        mapDefToGuid.Add(d, g);
                    }
                    else
                    {
                        Debug.Assert(d is InternalDefinition);

                        // this built-in parameter is INVALID:

                        BuiltInParameter bip = ((InternalDefinition)d).BuiltInParameter;
                        Debug.Print(d.Name + ": " + bip.ToString());

                        // if have a definition file and group name, we can still determine the GUID:

                        //Guid g = SharedParamGuid( app, "Identity data", d.Name );

                        mapDefToGuid.Add(d, null);
                    }
                }
            }

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

            if (!Util.GetSelectedElementsOrAll(
                    walls, uidoc, typeof(Wall)))
            {
                Selection sel = uidoc.Selection;
                message = (0 < sel.GetElementIds().Count)
        ? "Please select some wall elements."
        : "No wall elements found.";
            }
            else
            {
                //List<string> keys = new List<string>( mapDefToGuid.Keys );
                //keys.Sort();

                foreach (Wall wall in walls)
                {
                    Debug.Print(Util.ElementDescription(wall));

                    foreach (Definition d in mapDefToGuid.Keys)
                    {
                        object o = mapDefToGuid[d];

                        Parameter p = (null == o)
            ? wall.get_Parameter(d)
            : wall.get_Parameter((Guid)o);

                        string s = (null == p)
            ? "<null>"
            : p.AsValueString();

                        Debug.Print(d.Name + ": " + s);
                    }
                }
            }
            return(Result.Failed);
        }