New Family Type with New Values on its Parameters
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi! I'm BJ.
So I recently started learning Revit API and C#. I took some short course.
In order for me to practice what i learn, I thought of creating my own mini project.
My goal is to create a tool/plugin where I can create a new family type with same parameters (height, width, diameter, material, visibility, and description) but different value. (example. Height= 450mm then new family has 500mm). This will later on have a window form as its user interface.
But for now, i am focusing on building the correct code to start.
So I started with coding the selection of the element in the Revit file
then read the element ID
get its parameter value
and replace to a new one
but it seems like when i select the element placed. It doesn't read the intended parameter.
Is it possible to have personalize parameters to be adjusted?
Also, if i need to rework on the entire code, any suggestions, recommendations or comments are very much welcome and is a big big help. (The current code is like an initial code from www.revitapidocs.com and I tweaked it a little bit)
I have placed the code below and attached the sample test file for your reference.
Cheers,
BJ
namespace BJPracticePluginTools
{
[TransactionAttribute(TransactionMode.Manual)]
public class CreateNewType : IExternalCommand
{
public void EditFamilyTypes(Document document, FamilyInstance familyInstance)
{
// example works best when familyInstance is a rectangular concrete element
if ((null == document) || (null == familyInstance.Symbol))
{
return; // invalid arguments
}
// Get family associated with this
Family family = familyInstance.Symbol.Family;
if (null == family)
{
return; // could not get the family
}
// Get Family document for family
Document familyDoc = document.EditFamily(family);
if (null == familyDoc)
{
return; // could not open a family for edit
}
FamilyManager familyManager = familyDoc.FamilyManager;
if (null == familyManager)
{
return; // cuould not get a family manager
}
// Start transaction for the family document
using (Transaction newFamilyTypeTransaction = new Transaction(familyDoc, "Add Type to Family"))
{
int changesMade = 0;
newFamilyTypeTransaction.Start();
// add a new type and edit its parameters
FamilyType newFamilyType = familyManager.NewType("2X2");
if (newFamilyType != null)
{
// look for 'b' and 'h' parameters and set them to 2 feet
FamilyParameter familyParam = familyManager.get_Parameter("Height");
if (null != familyParam)
{
familyManager.Set(familyParam, 2000.0);
changesMade += 1;
}
familyParam = familyManager.get_Parameter("Width");
if (null != familyParam)
{
familyManager.Set(familyParam, 2000.0);
changesMade += 1;
}
}
if (2 == changesMade) // set both paramaters?
{
newFamilyTypeTransaction.Commit();
}
else // could not make the change -> should roll back
{
newFamilyTypeTransaction.RollBack();
}
// if could not make the change or could not commit it, we return
if (newFamilyTypeTransaction.GetStatus() != TransactionStatus.Committed)
{
return;
}
}
// now update the Revit project with Family which has a new type
LoadOpts loadOptions = new LoadOpts();
// This overload is necessary for reloading an edited family
// back into the source document from which it was extracted
family = familyDoc.LoadFamily(document, loadOptions);
if (null != family)
{
// find the new type and assign it to FamilyInstance
ISet<ElementId> familySymbolIds = family.GetFamilySymbolIds();
foreach (ElementId id in familySymbolIds)
{
FamilySymbol familySymbol = family.Document.GetElement(id) as FamilySymbol;
if ((null != familySymbol) && familySymbol.Name == "2X2")
{
using (Transaction changeSymbol = new Transaction(document, "Change Symbol Assignment"))
{
changeSymbol.Start();
familyInstance.Symbol = familySymbol;
changeSymbol.Commit();
}
break;
}
}
}
}
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
throw new NotImplementedException();
}
class LoadOpts : 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;
}
}
}
}