Exemplo n.º 1
0
        private bool WriteParameter(Element element, BCFParameters bcfParam, string value)
        {
            var updated = false;

            try
            {
                var param = element.LookupParameter(bcfParam.ToString());
                if (null != param)
                {
                    if (!param.IsReadOnly)
                    {
                        updated = param.Set(value);
                    }
                }
                else
                {
                    var insertedParam = InsertBinding(bcfParam, element.Category);
                    if (insertedParam)
                    {
                        updated = WriteParameter(element, bcfParam, value);
                    }
                }
            }
            catch (Exception)
            {
                // ignored
            }

            return(updated);
        }
Exemplo n.º 2
0
        public static bool UpdateBCFParameter(Document doc, ElementProperties ep, BCFParameters bcfParam, string value)
        {
            bool result = false;

            try
            {
                Element element = doc.GetElement(new ElementId(ep.ElementId));
                if (null != element)
                {
#if RELEASE2015 || RELEASE2016
                    Parameter param = element.LookupParameter(bcfParam.ToString());
#elif RELEASE2013 || RELEASE2014
                    Parameter param = element.get_Parameter(bcfParam.ToString());
#endif
                    if (null != param)
                    {
                        using (Transaction trans = new Transaction(doc))
                        {
                            trans.Start("Update Parameter");
                            try
                            {
                                result = param.Set(value);
                                trans.Commit();
                            }
                            catch (Exception ex)
                            {
                                string message = ex.Message;
                                trans.RollBack();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(bcfParam.ToString() + " Failed to update BCF parameter.\n" + ex.Message, "Update BCF Parameter", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(result);
        }
Exemplo n.º 3
0
        private bool InsertBinding(BCFParameters bcfParam, Category category)
        {
            var inserted = false;

            try
            {
                var        iter            = ActiveDoc.ParameterBindings.ForwardIterator();
                Definition definitionFound = null;
                while (iter.MoveNext())
                {
                    var definition  = iter.Key;
                    var elemBinding = (ElementBinding)iter.Current;
                    if (definition.Name == bcfParam.ToString())
                    {
                        definitionFound = definition;
                    }
                }

                if (category.AllowsBoundParameters)
                {
                    if (null != definitionFound)
                    {
                        var elementBinding = (ElementBinding)ActiveDoc.ParameterBindings.get_Item(definitionFound);
                        if (null != elementBinding)
                        {
                            var catset = elementBinding.Categories;
                            catset.Insert(category);

                            var binding = m_app.Application.Create.NewInstanceBinding(catset);
                            inserted = ActiveDoc.ParameterBindings.ReInsert(definitionFound, binding);
                        }
                    }
                    else
                    {
                        var originalDefinitionFile = m_app.Application.SharedParametersFilename;
                        m_app.Application.SharedParametersFilename = addinDefinitionFile;
                        var definitionFile = m_app.Application.OpenSharedParameterFile();
                        foreach (var group in definitionFile.Groups)
                        {
                            if (group.Name == "HOK BCF")
                            {
                                foreach (var definition in group.Definitions)
                                {
                                    if (definition.Name == bcfParam.ToString())
                                    {
                                        definitionFound = definition;
                                        break;
                                    }
                                }
                                break;
                            }
                        }

                        if (null != definitionFound)
                        {
                            var catset = m_app.Application.Create.NewCategorySet();
                            catset.Insert(category);

                            var binding = m_app.Application.Create.NewInstanceBinding(catset);
                            inserted = ActiveDoc.ParameterBindings.Insert(definitionFound, binding);
                        }

                        m_app.Application.SharedParametersFilename = originalDefinitionFile;
                    }
                }
            }
            catch (Exception ex)
            {
                var message = ex.Message;
            }
            return(inserted);
        }
Exemplo n.º 4
0
        private static Definition FindExistingDefinition(Document doc, BCFParameters bcfParam)
        {
            Definition definitionFound = null;

            try
            {
                switch (bcfParam)
                {
                case BCFParameters.BCF_ProjectId:
                    definitionFound = ProjectIdDefinition;
                    break;

                case BCFParameters.BCF_Action:
                    definitionFound = ActionDefinition;
                    break;

                case BCFParameters.BCF_Author:
                    definitionFound = AuthorDefinition;
                    break;

                case BCFParameters.BCF_Comment:
                    definitionFound = CommentDefinition;
                    break;

                case BCFParameters.BCF_Date:
                    definitionFound = DateDefinition;
                    break;

                case BCFParameters.BCF_Name:
                    definitionFound = NameDefinition;
                    break;

                case BCFParameters.BCF_Responsibility:
                    definitionFound = ResponsibilityDefinition;
                    break;

                case BCFParameters.BCF_Topic:
                    definitionFound = TopicDefinition;
                    break;
                }

                if (null == definitionFound)
                {
                    DefinitionBindingMapIterator iter = doc.ParameterBindings.ForwardIterator();
                    while (iter.MoveNext())
                    {
                        Definition     definition  = iter.Key;
                        ElementBinding elemBinding = (ElementBinding)iter.Current;
                        if (definition.Name == bcfParam.ToString())
                        {
                            definitionFound = definition;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(bcfParam.ToString() + ": Failed to find existing definition.\n" + ex.Message, "Find Exisiting Definition", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(definitionFound);
        }
Exemplo n.º 5
0
        private static bool InsertBinding(UIApplication uiapp, DefinitionFile definitionFile, BCFParameters bcfParam, List <BuiltInCategory> bltCategories)
        {
            bool inserted = false;

            try
            {
                Document doc = uiapp.ActiveUIDocument.Document;
                using (Transaction trans = new Transaction(doc))
                {
                    trans.Start("Insert Binding");
                    try
                    {
                        DefinitionBindingMapIterator iter = doc.ParameterBindings.ForwardIterator();
                        CategorySet catSet = uiapp.Application.Create.NewCategorySet();
                        foreach (BuiltInCategory bltCat in bltCategories)
                        {
                            Category category = doc.Settings.Categories.get_Item(bltCat);
                            if (null != category)
                            {
                                catSet.Insert(category);
                            }
                        }

                        //see if the project parameter already exists
                        Definition definitionFound = FindExistingDefinition(doc, bcfParam);
                        if (null != definitionFound)
                        {
                            ElementBinding elemBinding = (ElementBinding)doc.ParameterBindings.get_Item(definitionFound);
                            if (null != elemBinding)
                            {
                                bool reinsert = false;
                                foreach (Category cat in catSet)
                                {
                                    if (!elemBinding.Categories.Contains(cat))
                                    {
                                        reinsert = true;
                                    }
                                }

                                if (reinsert)
                                {
                                    InstanceBinding binding = uiapp.Application.Create.NewInstanceBinding(catSet);
                                    inserted = doc.ParameterBindings.ReInsert(definitionFound, binding);
                                    trans.Commit();
                                }
                                return(inserted);
                            }
                        }


                        Definition bcfDefinition = null;
                        foreach (DefinitionGroup group in definitionFile.Groups)
                        {
                            if (group.Name == "HOK BCF")
                            {
                                foreach (Definition definition in group.Definitions)
                                {
                                    if (definition.Name == bcfParam.ToString())
                                    {
                                        bcfDefinition = definition;
                                        break;
                                    }
                                }
                                break;
                            }
                        }

                        if (null != bcfDefinition)
                        {
                            InstanceBinding binding = uiapp.Application.Create.NewInstanceBinding(catSet);
                            inserted = doc.ParameterBindings.Insert(bcfDefinition, binding);
                        }
                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        string message = ex.Message;
                        trans.RollBack();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(bcfParam.ToString() + " Failed to insert binding." + ex.Message, "Insert Binding", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(inserted);
        }