在Revit API中使用“ Set()”方法定义一个参数的布尔值的正确方法是哪种?

时间:2019-07-08 10:16:55

标签: c# revit-api revit

我正在尝试使用一个宏在Revit中生成一些元素。当我尝试为任何生成的元素定义任何参数时,我使用Set()类中的Parameter方法。

当我尝试定义任何doubleintstring参数时,它可以正常工作。但是,当我尝试定义bool参数时,它不起作用。

我知道在Revit中,您必须将所有布尔参数定义为int,因此我将所有布尔参数在false时转换为0,在true时转换为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;
    }

这些是该宏所需的所有方法。我不知道为什么不使用布尔参数..有什么想法吗?

谢谢

1 个答案:

答案 0 :(得分:1)

好吧...。这真的很尴尬,但我必须发布答案才能为社区提供帮助。

正确的方法是强制引入的类型以确保该值是整数。如果您不强制使用,则会以double的形式引入。因此,正确的方法将是:

Parameter parameter = Get_Parameter(instance_test, "Top_Hook90_Right");
parameter.Set((int)1);