DynamoDS / DynamoRevit

Dynamo Libraries for Revit
https://dynamobim.org
334 stars 187 forks source link

Custom UI Node: Dynamo Stuck at "run start with new working range... " #1890

Open clovertong opened 6 years ago

clovertong commented 6 years ago

If this issue is not a bug report or improvement request, please check the Dynamo forum, and start a thread there to discuss your issue.

Dynamo version

1.3.2

Revit version

Revit 2016

Operating system

Windows 7

What did you do?

Change Geometry Range when working with Custom UI Nodes

What did you expect to see?

Able to perform functionally after change to right geometry range , should able to see the massage of " Run completed with new working range "

What did you see instead?

1) Dynamo stuck at "run start with new working range..." 2) Unable to close this file , need to close Dynamo 3) This particular file is not working anymore when open it again even if deleted all the nodes and restart Revit .

jnealb commented 6 years ago

@clovertong Can you please provide more detail: the Dynamo graph, custom nodes and any packages?

mjkkirschner commented 6 years ago

What do your custom UI nodes do? Do they create geometry? Can you share the code?

clovertong commented 6 years ago

Sorry for the late reply !

I realized that the problem is caused by on particular Custom Node only. In this Custom Node, I am trying to select a curve/ a group of curves from Revit and convert them to Dynamo Curves directly all in once. Unlike the built-in Select Model Element Node, my Selection Node need to use an additional button to update curve position and it won't remember the information after close the file, I am still trying my luck on this part, but not much progress. Anyway , I guess it is because the Revit connection causes the geometry range issue.

Here is how the custom node looks like: selectcurve

Here is the source Code , I didnt include the xaml file and NodeView CS file.

[NodeName("Select Curves")] [NodeDescription("Select Group of curves or Single Curve")] [NodeCategory("Test")] [OutPortNames("Curve[]")] [OutPortTypes("Curve[]")] [OutPortDescriptions("Curve[]")] [IsDesignScriptCompatible] public class SelectionButtonNode : NodeModel { private DelegateCommand _buttonValue; private DelegateCommand _updatePosition; private bool _toggle=true;

    public bool WarningToggle = true;
    public AssociativeNode FunctionCall { get; set; }
    public AssociativeNode SelectedCurves { get; set; }

    public bool Toggle
    {
        get { return _toggle; }
        set
        {
            _toggle = value;
            RaisePropertyChanged("Toggle");
            OnNodeModified(true);
        }
    }

    public DelegateCommand SelectElements
    {
        get { return _buttonValue; }
        set
        {
            _buttonValue = value;
            RaisePropertyChanged("SelectElements");
            OnNodeModified(true);
        }
    }

    public DelegateCommand UpdatePosition
    {
        get { return _updatePosition; }
        set
        {
            _updatePosition = value;
            RaisePropertyChanged("UpdatePosition");
            OnNodeModified(true);
        }
    }

    public SelectionButtonNode()
    {
        RegisterAllPorts();
        ArgumentLacing = LacingStrategy.CrossProduct;
        SelectElements = new DelegateCommand(Select);
        UpdatePosition = new DelegateCommand(Update);

}
    private void Select(object obj)
    {
        WarningToggle = false;
        var run = AstFactory.BuildBooleanNode(true);
        FunctionCall = AstFactory.BuildFunctionCall(
        new Func<bool, object>(CurveSelection.SelectSingleElement),
        new List<AssociativeNode> { run });

        ConvertToCurve();
        Toggle = false;
    }
    private void Update(object obj)
    {
        var run = AstFactory.BuildBooleanNode(true);
        FunctionCall = AstFactory.BuildFunctionCall(
        new Func<bool, object>(CurveSelection.SingleElementUpdate),
        new List<AssociativeNode> { run });

        ConvertToCurve();
        Toggle = false;
    }
    private void ConvertToCurve()
    {
        SelectedCurves = AstFactory.BuildFunctionCall(
            new Func<List<Element>, List<object>>(CurveSelection.ConvertElementToCurve),
            new List<AssociativeNode> { FunctionCall });

    }
    public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode> inputAstNodes)
    {
        Warning("Select a Curve Element", WarningToggle);

        return new[]
        {
           AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex(0),  SelectedCurves),
        };
    }
 }
    }   

public static class CurveSelection { private static UIDocument uidoc = DocumentManager.Instance.CurrentUIDocument; private static Document dbdoc => uidoc.Document; private static Reference reference; public static string refString=> reference.ConvertToStableRepresentation(dbdoc);

    public static object SelectSingleElement(bool run)
    {
        reference = uidoc.Selection.PickObject(ObjectType.Element);
        return Operations();
    }
    public static object SingleElementUpdate(bool run)
    {
        return Operations();
    }

    private static object Operations()
    {

        Reference stableRepresentation = Reference.ParseFromStableRepresentation(DocumentManager.Instance.CurrentDBDocument, refString); 

        Autodesk.Revit.DB.Element revitElement = dbdoc.GetElement(stableRepresentation);
        var element = revitElement.ToDSType(true);// convert to dynamo elements
        try
        {
            return GetElementsInGroup(element);
        }
        catch (Exception)
        {
            return element;
        }

    }

    public static List<Revit.Elements.Element> GetElementsInGroup(Revit.Elements.Element groupElement)
    {
        Autodesk.Revit.DB.Element UnwrappedElement = groupElement.InternalElement;
        Autodesk.Revit.DB.Group unwrapGroup = (Autodesk.Revit.DB.Group)UnwrappedElement;

        var memberList = new List<Revit.Elements.Element>();
        try
        {
            foreach (var member in unwrapGroup.GetMemberIds())
            {
                Autodesk.Revit.DB.Element element = unwrapGroup.Document.GetElement(member);
                memberList.Add(element.ToDSType(true));
            }
        }
        catch (Exception )
        {
            memberList.AddRange(new List<Revit.Elements.Element>());
        }

        return memberList;
    }

    //convert to Curves
    public static List<object> ConvertElementToCurve(List<Element> elements)
    {
        var dynamoGeos = new List<object>();
        foreach (var element in elements)
        {
          // var a= (Revit.Elements.CurveElement) element;
           // var obj = a.Curves;

          List<object> obj = element.Geometry().ToList();
            dynamoGeos.AddRange(obj);
        }          
        return dynamoGeos;
    }

}

`