在 Revit API 中使用 "Set()" 方法定义一个参数的布尔值的正确方法是什么?
Which is the correct way to define a boolean value of one parameter using "Set()" method in Revit API?
我正在尝试使用一个宏在 Revit 中生成一些元素。当我尝试为任何生成的元素定义任何参数时,我使用 Parameter
class 中的 Set()
方法。
当我尝试定义任何 double
、int
或 string
参数时,它工作正常。但是,当我尝试定义 bool
参数时,它不起作用。
我知道在 Revit 中,您必须将所有布尔参数定义为 int,因此我将所有布尔参数在假时转换为 0,在真时转换为 1。
public void Define_Parameter()
{
// I get the family.
Family family_test = Get_Family("STR_Top&BottomReinforcement_Fixed_pruebas");
// I get the symbols of the family.
FamilySymbol symbols_test = ActiveUIDocument.Document.GetElement(family_test.GetFamilySymbolIds().First()) as FamilySymbol;
// I initiate one transaction.
Transaction transaction_test = new Transaction(ActiveUIDocument.Document, "Test");
transaction_test.Start();
// I generate all elements requiered to generate a new family instance
Line line_test = Line.CreateBound(new XYZ(0, 10, ActiveUIDocument.ActiveView.Origin.Z), new XYZ(10, 10, ActiveUIDocument.ActiveView.Origin.Z));
FamilyInstance instance_test = ActiveUIDocument.Document.Create.NewFamilyInstance(line_test, symbols_test, ActiveUIDocument.ActiveView);
// I modify the boolean parameter.
Parameter parameter = Get_Parameter(instance_test, "Top_Hook90_Right");
parameter.Set(1);
transaction_test.Commit();
}
public static Family Get_Family(string Family_Name)
{
// I get all families of the model.
FilteredElementCollector filter = new FilteredElementCollector(Phantom.BIM.Revit.Recursos.Datos.Documento_Revit.Document);
List<Element> families = filter.OfClass(typeof(Family)).ToList();
// I go through the list of families and I try to get the one requested
foreach (Element family in families) if ((family as Family).Name == Family_Name) return family as Family;
// The family requested doesn't exists.
return null;
}
public static Parameter Get_Parameter(Element Host_Element, string Param_Name)
{
// I go through the list of parameters and I try to return the one requested.
foreach (Parameter param in Host_Element.Parameters) if (param.Definition.Name == Param_Name) return param;
// The parameter doesn't exists.
return null;
}
这些都是宏需要的方法。我不知道为什么不使用布尔参数。有什么想法吗?
谢谢
嗯....这真的很尴尬,但我必须 post 回答才能帮助社区。
正确的做法是强制引入的类型确保该值是一个整数。如果你不强求,它被引入为double
。所以,正确的方法是:
Parameter parameter = Get_Parameter(instance_test, "Top_Hook90_Right");
parameter.Set((int)1);
我正在尝试使用一个宏在 Revit 中生成一些元素。当我尝试为任何生成的元素定义任何参数时,我使用 Parameter
class 中的 Set()
方法。
当我尝试定义任何 double
、int
或 string
参数时,它工作正常。但是,当我尝试定义 bool
参数时,它不起作用。
我知道在 Revit 中,您必须将所有布尔参数定义为 int,因此我将所有布尔参数在假时转换为 0,在真时转换为 1。
public void Define_Parameter()
{
// I get the family.
Family family_test = Get_Family("STR_Top&BottomReinforcement_Fixed_pruebas");
// I get the symbols of the family.
FamilySymbol symbols_test = ActiveUIDocument.Document.GetElement(family_test.GetFamilySymbolIds().First()) as FamilySymbol;
// I initiate one transaction.
Transaction transaction_test = new Transaction(ActiveUIDocument.Document, "Test");
transaction_test.Start();
// I generate all elements requiered to generate a new family instance
Line line_test = Line.CreateBound(new XYZ(0, 10, ActiveUIDocument.ActiveView.Origin.Z), new XYZ(10, 10, ActiveUIDocument.ActiveView.Origin.Z));
FamilyInstance instance_test = ActiveUIDocument.Document.Create.NewFamilyInstance(line_test, symbols_test, ActiveUIDocument.ActiveView);
// I modify the boolean parameter.
Parameter parameter = Get_Parameter(instance_test, "Top_Hook90_Right");
parameter.Set(1);
transaction_test.Commit();
}
public static Family Get_Family(string Family_Name)
{
// I get all families of the model.
FilteredElementCollector filter = new FilteredElementCollector(Phantom.BIM.Revit.Recursos.Datos.Documento_Revit.Document);
List<Element> families = filter.OfClass(typeof(Family)).ToList();
// I go through the list of families and I try to get the one requested
foreach (Element family in families) if ((family as Family).Name == Family_Name) return family as Family;
// The family requested doesn't exists.
return null;
}
public static Parameter Get_Parameter(Element Host_Element, string Param_Name)
{
// I go through the list of parameters and I try to return the one requested.
foreach (Parameter param in Host_Element.Parameters) if (param.Definition.Name == Param_Name) return param;
// The parameter doesn't exists.
return null;
}
这些都是宏需要的方法。我不知道为什么不使用布尔参数。有什么想法吗?
谢谢
嗯....这真的很尴尬,但我必须 post 回答才能帮助社区。
正确的做法是强制引入的类型确保该值是一个整数。如果你不强求,它被引入为double
。所以,正确的方法是:
Parameter parameter = Get_Parameter(instance_test, "Top_Hook90_Right");
parameter.Set((int)1);