Example #1
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);
        }
Example #3
0
        public static bool CreateSharedProjectParameters(Document doc, string param, CategorySet catSet)
        {
            DefinitionFile sharedParamsFile = GetSharedParamsFile(doc);

            if (null == sharedParamsFile)
            {
                throw new Exception("Shared Paremeter file does not exist.\nSet or Create the project Shared Parameter file.");
            }

            DefinitionGroup sharedParamsGroup = GetCreateSharedParamsGroup(sharedParamsFile, "Group6Face");
            Definition      definition        = GetCreateSharedParamsDefinition(sharedParamsGroup, ParameterType.Text, param, true);

            BindingMap bindMap = doc.ParameterBindings;

            Autodesk.Revit.DB.Binding binding = bindMap.get_Item(definition);
            bindMap.ReInsert(definition, binding);

            InstanceBinding instanceBinding = doc.Application.Create.NewInstanceBinding(catSet);

            try
            {
                if (!doc.ParameterBindings.Insert(definition, instanceBinding))
                {
                    doc.ParameterBindings.ReInsert(definition, instanceBinding);
                }

                return(true);
            }
            catch (Exception ex)
            {
                throw new Exception("Uneable to create parameter bindings.\n" + ex.Message);
            }
        }
Example #4
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);
            }
        }
Example #5
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);
        }
Example #6
0
        private void AddSharedParameters(Application app, Document doc)
        {
            //Save the previous shared param file path
            string previousSharedParam = app.SharedParametersFilename;

            //Extract shared param to a txt file
            string tempPath = System.IO.Path.GetTempPath();
            string SPPath   = Path.Combine(tempPath, "bimsyncSharedParameter.txt");

            if (!File.Exists(SPPath))
            {
                //extract the familly
                List <string> files = new List <string>();
                files.Add("bimsyncSharedParameter.txt");
                Services.ExtractEmbeddedResource(tempPath, "bimsync.Resources", files);
            }

            //set the shared param file
            app.SharedParametersFilename = SPPath;

            //Define a category set containing the project properties
            CategorySet myCategorySet   = app.Create.NewCategorySet();
            Categories  categories      = doc.Settings.Categories;
            Category    projectCategory = categories.get_Item(BuiltInCategory.OST_ProjectInformation);

            myCategorySet.Insert(projectCategory);

            //Retrive shared parameters
            DefinitionFile myDefinitionFile = app.OpenSharedParameterFile();

            DefinitionGroup definitionGroup = myDefinitionFile.Groups.get_Item("bimsync");

            foreach (Definition paramDef in definitionGroup.Definitions)
            {
                // Get the BingdingMap of current document.
                BindingMap bindingMap = doc.ParameterBindings;

                //the parameter does not exist
                if (!bindingMap.Contains(paramDef))
                {
                    //Create an instance of InstanceBinding
                    InstanceBinding instanceBinding = app.Create.NewInstanceBinding(myCategorySet);

                    bindingMap.Insert(paramDef, instanceBinding, BuiltInParameterGroup.PG_IDENTITY_DATA);
                }
                //the parameter is not added to the correct categories
                else if (bindingMap.Contains(paramDef))
                {
                    InstanceBinding currentBinding = bindingMap.get_Item(paramDef) as InstanceBinding;
                    currentBinding.Categories = myCategorySet;
                    bindingMap.ReInsert(paramDef, currentBinding, BuiltInParameterGroup.PG_IDENTITY_DATA);
                }
            }

            //Reset to the previous shared parameters text file
            app.SharedParametersFilename = previousSharedParam;
            File.Delete(SPPath);
        }
Example #7
0
        bool IsInstance(SharedParameterElement sharedParameter)
        {
            Document   doc        = sharedParameter.Document;
            BindingMap bindingMap = doc.ParameterBindings;
            Binding    binding    = bindingMap.get_Item(sharedParameter.GetDefinition());

            if (binding is InstanceBinding)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #8
0
        private bool AddParameter(Element element)
        {
            bool instanceBindOK = false;

            try
            {
                DefinitionFile  definitionFile = m_app.Application.OpenSharedParameterFile();
                DefinitionGroup defGroup       = definitionFile.Groups.get_Item("HOK BCF");
                if (null == defGroup)
                {
                    defGroup = definitionFile.Groups.Create("HOK BCF");
                }

                foreach (string defName in bcfParameters)
                {
#if RELEASE2015
                    Parameter parameter = element.LookupParameter(defName);
#else
                    Parameter parameter = element.get_Parameter(defName);
#endif
                    if (null != parameter)
                    {
                        continue;
                    }

                    Definition definition = defGroup.Definitions.get_Item(defName);
                    if (null == definition)
                    {
#if RELEASE2015
                        ExternalDefinitonCreationOptions option = new ExternalDefinitonCreationOptions(defName, ParameterType.Text);
                        definition = defGroup.Definitions.Create(option);
#else
                        definition = defGroup.Definitions.Create(defName, ParameterType.Text);
#endif
                    }

                    BindingMap      bindingMap      = m_app.ActiveUIDocument.Document.ParameterBindings;
                    InstanceBinding instanceBinding = bindingMap.get_Item(definition) as InstanceBinding;
                    if (null != instanceBinding)
                    {
                        instanceBinding.Categories.Insert(element.Category);
                        instanceBindOK = bindingMap.ReInsert(definition, instanceBinding);
                    }
                    else
                    {
                        CategorySet categories = m_app.Application.Create.NewCategorySet();
                        categories.Insert(element.Category);
                        instanceBinding = m_app.Application.Create.NewInstanceBinding(categories);
                        instanceBindOK  = bindingMap.Insert(definition, instanceBinding, BuiltInParameterGroup.PG_TEXT);
                    }
                    if (!instanceBindOK)
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to add project parameters for element " + element.Name + "\n" + ex.Message, "CommandForm:AddParameter", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                instanceBindOK = false;
            }
            return(instanceBindOK);
        }
Example #9
0
        public static Parameter AddParameterBase(Document doc, Element element, Category category, string parameterName, int parameterSetId, ForgeTypeId specId)
        {
            bool        isElementType = (element is ElementType);
            Definitions definitions   = isElementType ? Importer.TheCache.TypeGroupDefinitions : Importer.TheCache.InstanceGroupDefinitions;

            bool       newlyCreated = false;
            Definition definition   = definitions.get_Item(parameterName);

            if (definition == null)
            {
                ExternalDefinitionCreationOptions option = new ExternalDefinitionCreationOptions(parameterName, specId);
                definition = definitions.Create(option);
                if (definition == null)
                {
                    Importer.TheLog.LogError(parameterSetId, "Couldn't create parameter: " + parameterName, false);
                    return(null);
                }
                newlyCreated = true;
            }

            Guid guid = (definition as ExternalDefinition).GUID;

            Parameter      parameter = null;
            ElementBinding binding   = null;
            bool           reinsert  = false;

            if (!newlyCreated)
            {
                BindingMap bindingMap = Importer.TheCache.GetParameterBinding(doc);
                binding  = bindingMap.get_Item(definition) as ElementBinding;
                reinsert = (binding != null);
            }

            if (binding == null)
            {
                if (isElementType)
                {
                    binding = new TypeBinding();
                }
                else
                {
                    binding = new InstanceBinding();
                }
            }

            // The binding can fail if we haven't identified a "bad" category above.  Use try/catch as a safety net.
            try
            {
                if (!reinsert || !binding.Categories.Contains(category))
                {
                    binding.Categories.Insert(category);

                    BindingMap bindingMap = Importer.TheCache.GetParameterBinding(doc);
                    if (reinsert)
                    {
                        bindingMap.ReInsert(definition, binding, BuiltInParameterGroup.PG_IFC);
                    }
                    else
                    {
                        bindingMap.Insert(definition, binding, BuiltInParameterGroup.PG_IFC);
                    }
                }

                parameter = element.get_Parameter(guid);
            }
            catch
            {
            }

            if (parameter == null)
            {
                Importer.TheLog.LogError(parameterSetId, "Couldn't create parameter: " + parameterName, false);
            }

            return(parameter);
        }
Example #10
0
        private void AddSharedParameters(Application app, Document doc, CategorySet myCategorySet)
        {
            //Save the previous shared param file path
            string previousSharedParam = app.SharedParametersFilename;

            //Extract shared param to a txt file
            string tempPath = System.IO.Path.GetTempPath();
            string SPPath   = Path.Combine(tempPath, "FileProperties.txt");

            if (!File.Exists(SPPath))
            {
                //extract the familly
                List <string> files = new List <string>();
                files.Add("FileProperties.txt");
                Tools.ExtractEmbeddedResource(tempPath, "TimeStamp.Resources", files);
            }

            //set the shared param file
            app.SharedParametersFilename = SPPath;

            //Retrive shared parameters
            DefinitionFile myDefinitionFile = app.OpenSharedParameterFile();

            DefinitionGroup definitionGroup = myDefinitionFile.Groups.get_Item("FileProperties");

            foreach (Definition paramDef in definitionGroup.Definitions)
            {
                // Get the BingdingMap of current document.
                BindingMap bindingMap = doc.ParameterBindings;

                //the parameter does not exist
                if (!bindingMap.Contains(paramDef))
                {
                    //Create an instance of InstanceBinding
                    InstanceBinding instanceBinding = app.Create.NewInstanceBinding(myCategorySet);

                    bindingMap.Insert(paramDef, instanceBinding, BuiltInParameterGroup.PG_IDENTITY_DATA);
                }
                //the parameter is not added to the correct categories
                else if (bindingMap.Contains(paramDef))
                {
                    InstanceBinding currentBinding = bindingMap.get_Item(paramDef) as InstanceBinding;
                    currentBinding.Categories = myCategorySet;
                    bindingMap.ReInsert(paramDef, currentBinding, BuiltInParameterGroup.PG_IDENTITY_DATA);
                }
            }

            //SetAllowVaryBetweenGroups for these parameters
            string[] parameterNames = { "BIM42_Date", "BIM42_Version", "BIM42_File", "BIM42_Discipline" };

            DefinitionBindingMapIterator definitionBindingMapIterator = doc.ParameterBindings.ForwardIterator();

            definitionBindingMapIterator.Reset();

            while (definitionBindingMapIterator.MoveNext())
            {
                InternalDefinition paramDef = definitionBindingMapIterator.Key as InternalDefinition;
                if (paramDef != null)
                {
                    if (parameterNames.Contains(paramDef.Name))
                    {
                        paramDef.SetAllowVaryBetweenGroups(doc, true);
                    }
                }
            }

            //Reset to the previous shared parameters text file
            app.SharedParametersFilename = previousSharedParam;
            File.Delete(SPPath);
        }
        /// <summary>
        ///  Creates new shared parameter if it does not exist and bind it to the type or instance of the objects
        /// </summary>
        /// <param name="paramType">Type of the parameter</param>
        /// <param name="categoriesForParam">Category of elements to bind the parameter to</param>
        /// <param name="defaultGroupName">Group name of the parameters</param>
        /// <param name="parameterName">Name of the parameter</param>
        /// <returns>TRUE if shared parameter is created, FALSE otherwise</returns>
        public static bool SetNewSharedParameter(this Element element, ParameterType paramType, CategorySet categoriesForParam, string parameterName, BuiltInParameterGroup group)
        {
            string defaultGroupName = "4P_imported_parameters"; //this is a hack to avoid multiple parameters with the same name.

            if (categoriesForParam == null)
            {
                return(false);
            }
            if (categoriesForParam.Size == 0)
            {
                return(false);
            }
            foreach (Category cat in categoriesForParam)
            {
                if (cat == null)
                {
                    return(false);
                }
                if (!cat.AllowsBoundParameters)
                {
                    return(false);
                }
            }

            Application application = element.Document.Application;
            Document    document    = element.Document;

            if (_definitionFile == null)
            {
                //ask for the location of the shared parameters file
                var dialog = new Microsoft.Win32.OpenFileDialog();
                dialog.CheckFileExists = false;
                dialog.Title           = "Set shared pID file...";
                dialog.ShowDialog();
                string shrFilePath = dialog.FileName;
                if (shrFilePath == null)
                {
                    _definitionFile = application.OpenSharedParameterFile();
                    if (_definitionFile == null)
                    {
                        SetSharedParamFileInUserProgramFolder(document);
                    }
                }
                else
                {
                    SetDefinitionFile(element, shrFilePath);
                }
            }
            if (_definitionFile == null)
            {
                throw new Exception("Definition file must be set before creation of the new parameters.");
            }

            DefinitionFile myDefinitionFile = _definitionFile;

            // Get parameter or create new one
            DefinitionGroups myGroups     = myDefinitionFile.Groups;
            Definition       myDefinition = null;
            bool             found        = false;

            foreach (DefinitionGroup gr in myGroups)
            {
                foreach (Definition def in gr.Definitions)
                {
                    if (def.Name == parameterName)
                    {
                        myDefinition = def;
                        found        = true;
                        break;
                    }
                }
                if (found)
                {
                    break;
                }
            }

            //if there is not such a parameter new one is created in default group
            if (myDefinition == null)
            {
                DefinitionGroup myGroup = myGroups.get_Item(defaultGroupName);
                if (myGroup == null)
                {
                    myGroup = myGroups.Create(defaultGroupName);
                }

                // Create a type definition
                myDefinition = myGroup.Definitions.get_Item(parameterName);
                if (myDefinition == null)
                {
                    myDefinition = myGroup.Definitions.Create(parameterName, paramType);
                }
            }


            //Create an object of TypeBinding or InstanceBinding according to the Categories and "typeBinding" variable
            Binding binding = null;
            // Get the BingdingMap of current document.
            BindingMap bindingMap = document.ParameterBindings;

            binding = bindingMap.get_Item(myDefinition);

            bool bindOK = false;

            if (!element.CanHaveTypeAssigned() && !(element is Material) && !(element is ProjectInfo))
            {
                if (binding != null)
                {
                    TypeBinding typeBinding = binding as TypeBinding;
                    if (typeBinding == null)
                    {
                        throw new Exception("Parameter with this definition already exists and is bound to instances. It cannot be bound to the type at the same time");
                    }
                    foreach (Category cat in categoriesForParam)
                    {
                        typeBinding.Categories.Insert(cat);
                    }
                    bindOK = bindingMap.ReInsert(myDefinition, binding, group);
                    return(bindOK);
                }
                else
                {
                    binding = application.Create.NewTypeBinding(categoriesForParam);
                }
            }
            else
            {
                if (binding != null)
                {
                    InstanceBinding instBinding = binding as InstanceBinding;
                    if (instBinding == null)
                    {
                        throw new Exception("Parameter with this definition already exists and is bound to types. It cannot be bound to the instance at the same time");
                    }
                    foreach (Category cat in categoriesForParam)
                    {
                        instBinding.Categories.Insert(cat);
                    }
                    bindOK = bindingMap.ReInsert(myDefinition, binding, group);
                    return(bindOK);
                }
                else
                {
                    binding = application.Create.NewInstanceBinding(categoriesForParam);
                }
            }



            // Bind the definitions to the document
            bindOK = bindingMap.Insert(myDefinition, binding, group);
            return(bindOK);
        }