[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
// [Journaling(JournalingMode.NoCommandData)]
public class NewFamily : IExternalCommand
{
// 毫米到英寸,单位换算
public double mmToFeet( double val) { return val / 304.8 ; }
// 通过族名称,族类型,从过滤器中找到指定的类型。
public Element FindElementType(Document doc, Type targetType, string familyName, string typeName, BuiltInCategory catagory)
{
IList < Element > elements = FindSpecialElements(doc, targetType, catagory);
Element elem = null ;
foreach (Element e in elements)
{
if (e.Name.Equals(typeName) &&
e.get_Parameter(BuiltInParameter.SYMBOL_FAMILY_NAME_PARAM).AsString().Equals(familyName))
{
elem = e;
break ;
}
}
return elem;
}
// 过滤
// FindSpecialElements(doc, typeof(FamilySymbol), BuiltInCategory.OST_Doors);
public IList < Element > FindSpecialElements(Document doc, Type targetType, BuiltInCategory catagory)
{
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfClass(targetType);
if (collector != null )
collector.OfCategory(catagory);
IList < Element > elements = collector.ToElements();
return elements;
}
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Autodesk.Revit.ApplicationServices.Application app = commandData.Application.Application;
//
HyRevit.Key.Press.Keys( " DR " );
string family = " 单开门 " ;
string type = " M0721 " ;
FamilySymbol fs = FindElementType(uiDoc.Document, typeof (FamilySymbol), family, type, BuiltInCategory.OST_Doors) as FamilySymbol;
Family fy = fs.Family;
Document familyDoc = uiDoc.Document.EditFamily(fy);
if ( null != familyDoc)
{
if (familyDoc.IsFamilyDocument)
{
FamilyManager familyManager = familyDoc.FamilyManager;
if ( null != familyManager)
{
Transaction tr = new Transaction(familyDoc, " CreateDoorFamilyTransaction " );
tr.Start();
SubTransaction subTransaction = new SubTransaction(familyDoc);
subTransaction.Start();
FamilyType newFamilyType = familyManager.NewType( " 新门1001 " );
FamilyParameter familyParam = familyManager.get_Parameter( " Width " );
if ( null != familyParam)
{
familyManager.Set(familyParam, mmToFeet(Convert.ToDouble( " 500 " )));
}
familyParam = familyManager.get_Parameter( " Height " );
if ( null != familyParam)
{
familyManager.Set(familyParam, mmToFeet(Convert.ToDouble( " 2000 " )));
}
familyDoc.LoadFamily(uiDoc.Document, new FamilyOptions());
subTransaction.Commit();
tr.Commit();
familyDoc.Close( false );
familyDoc.Dispose();
}
}
}
// 进入命令,设置默认
family = " 单开门 " ;
type = " 新门1001 " ;
FamilySymbol fsNew = FindElementType(uiDoc.Document, typeof (FamilySymbol), family, type, BuiltInCategory.OST_Doors) as FamilySymbol;
uiDoc.PromptForFamilyInstancePlacement(fsNew);
return Result.Succeeded;
}
}
[Regeneration(RegenerationOption.Manual)]
// [Journaling(JournalingMode.NoCommandData)]
public class NewFamily : IExternalCommand
{
// 毫米到英寸,单位换算
public double mmToFeet( double val) { return val / 304.8 ; }
// 通过族名称,族类型,从过滤器中找到指定的类型。
public Element FindElementType(Document doc, Type targetType, string familyName, string typeName, BuiltInCategory catagory)
{
IList < Element > elements = FindSpecialElements(doc, targetType, catagory);
Element elem = null ;
foreach (Element e in elements)
{
if (e.Name.Equals(typeName) &&
e.get_Parameter(BuiltInParameter.SYMBOL_FAMILY_NAME_PARAM).AsString().Equals(familyName))
{
elem = e;
break ;
}
}
return elem;
}
// 过滤
// FindSpecialElements(doc, typeof(FamilySymbol), BuiltInCategory.OST_Doors);
public IList < Element > FindSpecialElements(Document doc, Type targetType, BuiltInCategory catagory)
{
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfClass(targetType);
if (collector != null )
collector.OfCategory(catagory);
IList < Element > elements = collector.ToElements();
return elements;
}
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Autodesk.Revit.ApplicationServices.Application app = commandData.Application.Application;
//
HyRevit.Key.Press.Keys( " DR " );
string family = " 单开门 " ;
string type = " M0721 " ;
FamilySymbol fs = FindElementType(uiDoc.Document, typeof (FamilySymbol), family, type, BuiltInCategory.OST_Doors) as FamilySymbol;
Family fy = fs.Family;
Document familyDoc = uiDoc.Document.EditFamily(fy);
if ( null != familyDoc)
{
if (familyDoc.IsFamilyDocument)
{
FamilyManager familyManager = familyDoc.FamilyManager;
if ( null != familyManager)
{
Transaction tr = new Transaction(familyDoc, " CreateDoorFamilyTransaction " );
tr.Start();
SubTransaction subTransaction = new SubTransaction(familyDoc);
subTransaction.Start();
FamilyType newFamilyType = familyManager.NewType( " 新门1001 " );
FamilyParameter familyParam = familyManager.get_Parameter( " Width " );
if ( null != familyParam)
{
familyManager.Set(familyParam, mmToFeet(Convert.ToDouble( " 500 " )));
}
familyParam = familyManager.get_Parameter( " Height " );
if ( null != familyParam)
{
familyManager.Set(familyParam, mmToFeet(Convert.ToDouble( " 2000 " )));
}
familyDoc.LoadFamily(uiDoc.Document, new FamilyOptions());
subTransaction.Commit();
tr.Commit();
familyDoc.Close( false );
familyDoc.Dispose();
}
}
}
// 进入命令,设置默认
family = " 单开门 " ;
type = " 新门1001 " ;
FamilySymbol fsNew = FindElementType(uiDoc.Document, typeof (FamilySymbol), family, type, BuiltInCategory.OST_Doors) as FamilySymbol;
uiDoc.PromptForFamilyInstancePlacement(fsNew);
return Result.Succeeded;
}
}