Exemplo n.º 1
0
        /// <summary>
        /// Sort parameters of families which have been loaded into a project.
        /// </summary>
        /// <param name="order">Ascending or Descending</param>
        private void SortParameters(ParametersOrder order)
        {
            try
            {
                FilteredElementCollector coll     = new FilteredElementCollector(m_currentDoc);
                IList <Element>          families = coll.OfClass(typeof(Family)).ToElements();

                // Edit each family->sort parameters order->save to a new file->load back to the document.
                int count = 0;
                foreach (Family fam in families)
                {
                    if (!fam.IsEditable)
                    {
                        continue;
                    }

                    Document famDoc = m_currentDoc.EditFamily(fam);

                    using (Transaction trans = new Transaction(famDoc, "Sort parameters."))
                    {
                        trans.Start();
                        famDoc.FamilyManager.SortParameters(order);
                        trans.Commit();
                    }

                    string tmpFile = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), fam.Name + ".rfa");

                    if (File.Exists(tmpFile))
                    {
                        File.Delete(tmpFile);
                    }

                    famDoc.SaveAs(tmpFile);
                    famDoc.Close(false);

                    using (Transaction trans = new Transaction(m_currentDoc, "Load family."))
                    {
                        trans.Start();
                        IFamilyLoadOptions famLoadOptions = new FamilyLoadOptions();
                        Family             newFam         = null;
                        m_currentDoc.LoadFamily(tmpFile, new FamilyLoadOptions(), out newFam);
                        trans.Commit();
                    }

                    File.Delete(tmpFile);
                    count++;
                }

                TaskDialog.Show("Message", "Sort completed! " + count.ToString() + " families sorted.");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error:" + ex.Message);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sort parameters' order in family files which is located in a folder, the new files are saved in subfolder named "ordered".
        /// </summary>
        /// <param name="order">Ascending or Descending.</param>
        private void SortParameters(ParametersOrder order)
        {
            // Convert relative path to absolute path.
            string absPath = directoryTxt.Text;

            if (!Path.IsPathRooted(absPath))
            {
                absPath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), absPath);
            }

            DirectoryInfo dirInfo = new DirectoryInfo(absPath);

            if (!dirInfo.Exists)
            {
                MessageBox.Show("Please select a valid directory first.");
                return;
            }

            string orderedDir = Path.Combine(absPath, "ordered");

            if (!Directory.Exists(orderedDir))
            {
                Directory.CreateDirectory(orderedDir);
            }

            // Sort parameters in each family file.
            FileInfo[] fileInfo = dirInfo.GetFiles("*.rfa");
            foreach (FileInfo fInfo in fileInfo)
            {
                Document doc = m_uiApp.Application.OpenDocumentFile(fInfo.FullName);
                using (Transaction trans = new Transaction(doc, "Sort parameters."))
                {
                    trans.Start();
                    doc.FamilyManager.SortParameters(order);
                    trans.Commit();
                }

                string destFile = Path.Combine(orderedDir, fInfo.Name);
                if (File.Exists(destFile))
                {
                    File.Delete(destFile);
                }

                doc.SaveAs(destFile);
                doc.Close(false);
            }

            TaskDialog.Show("Message", "Sort completed! " + fileInfo.Count().ToString() + " family file(s) sorted.");
        }