//
// (C) Copyright 2003-2015 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace Revit.SDK.Samples.FamilyParametersOrder.CS
{
///
/// Sort parameters' order in families which are loaded into a project or a family:
///
/// - "A–>Z" button will update parameters in each loaded family to alphabet order then reload the family to project.
/// - "Z–>A" button will update parameters in each loaded family to reverse alphabet order then reload the family to project.
/// - "Close" button will close the whole dialog.
///
///
public partial class SortLoadedFamiliesParamsForm : System.Windows.Forms.Form
{
///
/// Document whose loaded families parameters' order want to be sort.
///
private Document m_currentDoc;
///
/// Construct with a Document.
///
///
public SortLoadedFamiliesParamsForm(Document currentDoc)
{
m_currentDoc = currentDoc;
InitializeComponent();
}
///
/// Sort parameters of families which have been loaded into a project.
///
/// Ascending or Descending
private void SortParameters(ParametersOrder order)
{
try
{
FilteredElementCollector coll = new FilteredElementCollector(m_currentDoc);
IList 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);
}
}
///
/// Sort families parameters with ascending.
///
/// Not used.
/// Not used.
private void A_ZBtn_Click(object sender, EventArgs e)
{
SortParameters(ParametersOrder.Ascending);
}
///
/// Sort families parameters with descending.
///
/// Not used.
/// Not used.
private void Z_ABtn_Click(object sender, EventArgs e)
{
SortParameters(ParametersOrder.Descending);
}
///
/// Close this dialog.
///
/// Not used.
/// Not used.
private void closeBtn_Click(object sender, EventArgs e)
{
Close();
}
private class FamilyLoadOptions : IFamilyLoadOptions
{
public bool OnFamilyFound(bool familyInUse, out bool overwriteParameterValues)
{
overwriteParameterValues = true;
return true;
}
public bool OnSharedFamilyFound(Family sharedFamily, bool familyInUse, out FamilySource source, out bool overwriteParameterValues)
{
source = FamilySource.Family;
overwriteParameterValues = true;
return true;
}
}
}
}