// ======================================
        //   (4.1) add formula
        // ======================================
        public void addFormulas()
        {
            // we will add the following fomulas
              //   Tw = Width / 4.0
              //   Td = Depth / 4.0
              //

              // first get the parameter
              FamilyManager pFamilyMgr = _doc.FamilyManager;

              FamilyParameter paramTw = pFamilyMgr.get_Parameter("Tw");
              FamilyParameter paramTd = pFamilyMgr.get_Parameter("Td");

              // set the formula
              pFamilyMgr.SetFormula(paramTw, "Width / 4.0");
              pFamilyMgr.SetFormula(paramTd, "Depth / 4.0");
        }
Exemple #2
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication rvtUIAPP = commandData.Application;
            UIDocument    uidoc    = rvtUIAPP.ActiveUIDocument;
            Configuration config   = ConfigurationManager.OpenExeConfiguration
                                         (System.Reflection.Assembly.GetExecutingAssembly().Location);
            string familyRepo = config.AppSettings.Settings["Family_repo_folder"].Value;
            string serverAPI  = config.AppSettings.Settings["Server_API"].Value;

            m_rvtDoc = uidoc.Document;
            m_rvtApp = rvtUIAPP.Application;
            try
            {
                string[] filePaths = Directory.GetFiles(@familyRepo, "*.rfa");
                foreach (string path in filePaths)
                {
                    Document doc = m_rvtApp.OpenDocumentFile(path);
                    if (doc.IsFamilyDocument)
                    {
                        try {
                            Family          f       = doc.OwnerFamily;
                            FamilyManager   manager = doc.FamilyManager;
                            FamilyParameter keynote = null;

                            String note = HttpGET(serverAPI + "GetFamilyKeynote?path=" + path);
                            keynote = manager.get_Parameter(BuiltInParameter.KEYNOTE_PARAM);
                            if (keynote != null)
                            {
                                using (Transaction trans = new Transaction(doc, "SET_PARAM"))
                                {
                                    trans.Start();
                                    if (manager.Types.Size == 0)
                                    {
                                        manager.NewType("Type 1");
                                    }
                                    manager.SetFormula(keynote, null);
                                    manager.Set(keynote, note);
                                    trans.Commit();
                                }
                                doc.Save();
                            }
                        }
                        catch (Exception e)
                        {
                            TaskDialog.Show("Error2", e.Message);
                        }
                    }
                }

                return(Result.Succeeded);
            }
            catch (Exception e)
            {
                message = e.Message;
                return(Result.Failed);
            }
        }
Exemple #3
0
        /// <summary>
        /// Set the formula of a family parameter (syntax is exactly as Revit, whatever works in Revit's formulas works here).
        /// </summary>
        /// <param name="parameterName">The family parameter.</param>
        /// <param name="formula">The formula string.</param>
        /// <returns>The document family</returns>
        public FamilyDocument SetFormula(string parameterName, string formula)
        {
            Autodesk.Revit.DB.FamilyParameter familyParameter = FamilyManager.get_Parameter(parameterName);
            if (familyParameter == null)
            {
                throw new InvalidOperationException(Properties.Resources.ParameterNotFound);
            }

            TransactionManager.Instance.EnsureInTransaction(this.InternalDocument);
            FamilyManager.SetFormula(familyParameter, formula);
            TransactionManager.Instance.TransactionTaskDone();
            return(this);
        }
Exemple #4
0
        private void SetParameters(UIApplication uiApp, string familyFile, ExternalDefinition externalDefinition)
        {
            try
            {
                //Get the FileInfo of the family file and then get the LastWriteTime value as a date string in MM/DD/YYYY format
                FileInfo fileInfo     = new FileInfo(familyFile);
                string   lastModified = fileInfo.LastWriteTime.ToShortDateString();

                //Open the family file
                RVTDocument famDoc = RVTOperations.OpenRevitFile(uiApp, familyFile);
                if (famDoc != null)
                {
                    //Get the family manager for the family file
                    FamilyManager famMan = famDoc.FamilyManager;
                    //Get the family parameters and add them to a dictionary indexed by parameter name
                    FamilyParameter famParameter = null;
                    Dictionary <string, FamilyParameter> famParamDict = new Dictionary <string, FamilyParameter>();
                    foreach (FamilyParameter famParam in famMan.Parameters)
                    {
                        famParamDict.Add(famParam.Definition.Name, famParam);
                    }

                    //Get the number of family types because there must be at least one family type to make this work
                    FamilyTypeSet types         = famMan.Types;
                    int           numberOfTypes = types.Size;

                    Transaction t1 = new Transaction(famDoc, "SetParameters");
                    t1.Start();
                    if (numberOfTypes == 0)
                    {
                        //If the number of family types was 0, then one must be made. Thus Default is a family type created
                        try
                        {
                            famMan.NewType("Default");
                        }
                        catch { MessageBox.Show(String.Format("Could not make a default type or find any type for {0}", familyFile)); }
                    }

                    //Once the existence of a family type is confirmed, move on with determining if the version parameter already exists. If it doesn't add the parameter to the family and regenerate the document
                    if (!famParamDict.Keys.Contains(BARevitTools.Properties.Settings.Default.RevitUFVPParameter))
                    {
                        famParameter = famMan.AddParameter(externalDefinition, BuiltInParameterGroup.PG_IDENTITY_DATA, false);
                        famDoc.Regenerate();
                    }
                    else
                    {
                        famParameter = famParamDict[BARevitTools.Properties.Settings.Default.RevitUFVPParameter];
                    }

                    //Check to see if the value for the parameter is equal to the date last modified
                    if (famMan.CurrentType.AsString(famParameter) == lastModified)
                    {
                        //If so, roll back the transaction and just close the file.
                        t1.RollBack();
                        famDoc.Close(false);
                    }
                    else
                    {
                        //Otherwise set the formula of the version parameter to the quote encapsulated date, just like it would appear in Revit, commit it, then save.
                        famMan.SetFormula(famParameter, "\"" + lastModified + "\"");
                        t1.Commit();
                        RVTOperations.SaveRevitFile(uiApp, famDoc, true);
                    }
                }
                else
                {
                    //If the family could not be opened for any reason, let the user know
                    MessageBox.Show(String.Format("{0} could not be opened.", familyFile));
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }