private void bttnCreate_Click(object sender, EventArgs e)
        {
            Definition definition = null;

            using (Transaction trans = new Transaction(m_doc))
            {
                trans.Start("Create Shared Parameter");
                try
                {
                    defDictionary.Clear();

                    DefinitionGroups dGroups = definitionFile.Groups;
                    DefinitionGroup  dGroup  = dGroups.get_Item("HOK Mass Parameters");
                    if (null == dGroup)
                    {
                        dGroup = dGroups.Create("HOK Mass Parameters");
                    }
                    Definitions definitions = dGroup.Definitions;

                    foreach (ListViewItem item in listViewMassParameter.Items)
                    {
                        definition = definitions.get_Item("Mass_" + item.Text);
                        Parameter parameter = item.Tag as Parameter;

                        if (null == definition && null != parameter)
                        {
                            ParameterType paramType = parameter.Definition.ParameterType;
#if RELEASE2013 || RELEASE2014
                            definition = definitions.Create("Mass_" + parameter.Definition.Name, paramType);
#elif RELEASE2015
                            ExternalDefinitonCreationOptions options = new ExternalDefinitonCreationOptions("Mass_" + parameter.Definition.Name, paramType);
                            definition = definitions.Create(options);
#elif RELEASE2016
                            ExternalDefinitionCreationOptions options = new ExternalDefinitionCreationOptions("Mass_" + parameter.Definition.Name, paramType);
                            definition = definitions.Create(options);
#endif
                        }

                        if (!defDictionary.ContainsKey(parameter.Definition.Name))
                        {
                            defDictionary.Add(parameter.Definition.Name, definition);
                        }
                    }
                    trans.Commit();
                    this.DialogResult = DialogResult.OK;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failed to set shared parameters.\n" + ex.Message, "Form_Parameters:CreateButtonClick", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    trans.RollBack();
                }
            }
        }
Exemple #2
0
        public static Dictionary <string, List <ParameterType> > GroupParameterTypesOnDiscipline()
        {
            Dictionary <string, List <ParameterType> > disciplineToParameterTypes = new Dictionary <string, List <ParameterType> >();

            string oriFile  = ThisApplication.CachedApp.SharedParametersFilename;
            string tempFile = Path.GetTempFileName() + ".txt";

            try
            {
                using (File.Create(tempFile)) { }
                ThisApplication.CachedApp.SharedParametersFilename = tempFile;

                Definitions tempDefinitions = ThisApplication.CachedApp.OpenSharedParameterFile().Groups.Create("TemporaryDefintionGroup").Definitions;
                foreach (ParameterType pt in Enum.GetValues(typeof(ParameterType)))
                {
                    if (pt != ParameterType.Invalid)
                    {
                        ExternalDefinitionCreationOptions op = new ExternalDefinitionCreationOptions(pt.ToString(), pt);
                        Definition def = tempDefinitions.Create(op);
                        UnitGroup  ug  = UnitUtils.GetUnitGroup(def.UnitType);
                        if (disciplineToParameterTypes.ContainsKey(ug.ToString()))
                        {
                            disciplineToParameterTypes[ug.ToString()].Add(pt);
                        }
                        else
                        {
                            disciplineToParameterTypes.Add(ug.ToString(), new List <ParameterType> {
                                pt
                            });
                        }
                    }
                }

                File.Delete(tempFile);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString(), "GroupParameterTypesOnDiscipline");
            }
            finally
            {
                ThisApplication.CachedApp.SharedParametersFilename = oriFile;
            }

            return(disciplineToParameterTypes);
        }
        public static ExternalDefinition AddParameterToDefFile(DefinitionFile defFile, string groupName, MyProjectSharedParameter myparam)
        {
            DefinitionGroup        tempGroup = null;
            List <DefinitionGroup> groups    = defFile.Groups.Where(i => i.Name == groupName).ToList();

            if (groups.Count == 0)
            {
                try
                {
                    tempGroup = defFile.Groups.Create(groupName);
                }
                catch (Exception ex)
                {
                    throw new Exception("Не удалось создать группу " + groupName + " в файле общих параметров " + defFile.Filename);
                }
            }
            else
            {
                tempGroup = groups.First();
            }


            Definitions defs = tempGroup.Definitions;
            ExternalDefinitionCreationOptions defOptions =
                new ExternalDefinitionCreationOptions(myparam.Name, myparam.def.ParameterType);

            defOptions.GUID = myparam.guid;

            ExternalDefinition exDef = defs.Create(defOptions) as ExternalDefinition;

            if (exDef == null)
            {
                throw new Exception("Не удалось создать общий параметр " + myparam.Name);
            }
            return(exDef);
        }
Exemple #4
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);
        }
Exemple #5
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Document famdoc = commandData.Application.ActiveUIDocument.Document;

            Autodesk.Revit.ApplicationServices.Application app = commandData.Application.Application;
            string oldSharedParamFilePath = app.SharedParametersFilename;

            if (!famdoc.IsFamilyDocument)
            {
                message = "Инструмнет доступен только в редакторе семейств";
                return(Result.Failed);
            }

            System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
            openDialog.Title       = "Выберите семейство-аналог";
            openDialog.Multiselect = false;

            if (openDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return(Result.Cancelled);
            }

            string familyPath = openDialog.FileName;

            Document           analogFamilyDoc           = commandData.Application.Application.OpenDocumentFile(familyPath);
            ParametersInFamily pif                       = new ParametersInFamily(analogFamilyDoc);
            List <SharedParameterContainer> analogParams = pif.parameters;


            string     familyFolder = Path.GetDirectoryName(familyPath);
            string     familyTitle  = analogFamilyDoc.Title.Remove(analogFamilyDoc.Title.Length - 4);
            string     txtPath      = Path.Combine(familyFolder, familyTitle + ".txt");
            FileStream fs           = File.Create(txtPath);

            fs.Close();

            commandData.Application.Application.SharedParametersFilename = txtPath;
            DefinitionFile defFile = app.OpenSharedParameterFile();

            foreach (SharedParameterContainer spc in analogParams)
            {
                DefinitionGroup tempGroup = defFile.Groups.Create(spc.name);
                Definitions     defs      = tempGroup.Definitions;
                ExternalDefinitionCreationOptions defOptions = new ExternalDefinitionCreationOptions(spc.name, spc.intDefinition.ParameterType);
                defOptions.GUID = spc.guid;

                spc.exDefinition = defs.Create(defOptions) as ExternalDefinition;
            }

            FamilyManager fMan = famdoc.FamilyManager;

            int    c = 0, ex = 0, er = 0;
            string errMsg = "";

            foreach (SharedParameterContainer spc in analogParams)
            {
                bool checkExists = ParametersInFamily.ParamIsExists(famdoc, spc);
                if (checkExists)
                {
                    ex++;
                    continue;
                }
                try
                {
                    using (Transaction t = new Transaction(famdoc))
                    {
                        t.Start("Добавление параметров");
                        fMan.AddParameter(spc.exDefinition, spc.paramGroup, spc.isInstance);
                        t.Commit();
                    }
                    c++;
                }
                catch (Exception exc)
                {
                    er++;
                    errMsg = exc.Message;
                }
            }



            analogFamilyDoc.Close(false);
            System.IO.File.Delete(txtPath);
            app.SharedParametersFilename = oldSharedParamFilePath;

            string msg = "Успешно добавлено параметров: " + c;

            if (ex > 0)
            {
                msg += "\nУже присутствовали в семействе: " + ex;
            }
            if (er > 0)
            {
                msg += "\nНе удалось добавить: " + er + "\n" + errMsg;
            }
            TaskDialog.Show("Добавление параметров", msg);

            return(Result.Succeeded);
        }
        private void StoreSharedParams()
        {
            using (Transaction trans = new Transaction(doc))
            {
                trans.Start("Store Shared Parameters");
                try
                {
                    string currentAssembly = System.Reflection.Assembly.GetAssembly(this.GetType()).Location;
                    string definitionPath  = Path.GetDirectoryName(currentAssembly) + "/Resources/Mass Shared Parameters.txt";
                    m_app.SharedParametersFilename = definitionPath;
                    definitionFile = m_app.OpenSharedParameterFile();

                    FilteredElementCollector collector = new FilteredElementCollector(doc);
                    Element       element            = null;
                    List <string> activeSharedParams = new List <string>();

                    switch (massCategory)
                    {
                    case MassCategory.Rooms:
                        element            = collector.OfCategory(BuiltInCategory.OST_Rooms).WhereElementIsNotElementType().ToElements().First();
                        activeSharedParams = roomSharedParameters;
                        break;

                    case MassCategory.Areas:
                        element            = collector.OfCategory(BuiltInCategory.OST_Areas).WhereElementIsNotElementType().ToElements().First();
                        activeSharedParams = areaSharedParameters;
                        break;

                    case MassCategory.Floors:
                        element            = collector.OfCategory(BuiltInCategory.OST_Floors).WhereElementIsNotElementType().ToElements().First();
                        activeSharedParams = floorSharedParameters;
                        break;
                    }

                    if (null != definitionFile)
                    {
                        DefinitionGroups dGroups = definitionFile.Groups;
                        DefinitionGroup  dGroup  = dGroups.get_Item("HOK Mass Parameters");
                        if (null == dGroup)
                        {
                            dGroup = dGroups.Create("HOK Mass Parameters");
                        }
                        Definitions definitions = dGroup.Definitions;
                        Definition  definition;

                        foreach (string paramName in activeSharedParams)
                        {
                            definition = definitions.get_Item("Mass_" + paramName);
                            if (null == definition)
                            {
#if RELEASE2013 || RELEASE2014
                                Parameter param = element.get_Parameter(paramName);
                                definition = definitions.Create("Mass_" + param.Definition.Name, param.Definition.ParameterType);
#elif RELEASE2015
                                Parameter param = element.LookupParameter(paramName);
                                ExternalDefinitonCreationOptions options = new ExternalDefinitonCreationOptions("Mass_" + param.Definition.Name, param.Definition.ParameterType);
                                definition = definitions.Create(options);
#elif RELEASE2016
                                Parameter param = element.LookupParameter(paramName);
                                ExternalDefinitionCreationOptions options = new ExternalDefinitionCreationOptions("Mass_" + param.Definition.Name, param.Definition.ParameterType);
                                definition = definitions.Create(options);
#endif
                            }
                            if (null != definition && !defDictionary.ContainsKey(paramName))
                            {
                                defDictionary.Add(paramName, definition);
                            }
                        }
                    }
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failed to store shared parameters.\n" + ex.Message, "Form_RoomMass:StoreSharedParams", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    trans.RollBack();
                }
            }
        }