Example #1
0
        public void SelectElementsASTGeneration()
        {
            var refPoints = new List<ReferencePoint>();

            using (var trans = new Transaction(DocumentManager.Instance.CurrentDBDocument, "Create some ReferencePoints"))
            {
                trans.Start();

                FailureHandlingOptions fails = trans.GetFailureHandlingOptions();
                fails.SetClearAfterRollback(true);
                trans.SetFailureHandlingOptions(fails);

                refPoints.Add(DocumentManager.Instance.CurrentDBDocument.FamilyCreate.NewReferencePoint(new XYZ(0,0,0)));
                refPoints.Add(DocumentManager.Instance.CurrentDBDocument.FamilyCreate.NewReferencePoint(new XYZ(0,0,1)));
                refPoints.Add(DocumentManager.Instance.CurrentDBDocument.FamilyCreate.NewReferencePoint(new XYZ(0,0,2)));

                trans.Commit();
            }

            var sel = new DSModelElementsSelection { SelectedElement = refPoints.Cast<Element>().Select(x => x.UniqueId).ToList() };

            var buildOutput = sel.BuildOutputAst(new List<AssociativeNode>());
            var funCall = (ExprListNode)((BinaryExpressionNode)buildOutput.First()).RightNode;

            Assert.AreEqual(funCall.list.Count, 3);
            Assert.Inconclusive("Need more robust testing here.");
        }
Example #2
0
        public void SelectElementASTGeneration()
        {
            ReferencePoint refPoint;

            using (var trans = new Transaction(DocumentManager.Instance.CurrentDBDocument, "CreateAndDeleteAreReferencePoint"))
            {
                trans.Start();

                FailureHandlingOptions fails = trans.GetFailureHandlingOptions();
                fails.SetClearAfterRollback(true);
                trans.SetFailureHandlingOptions(fails);

                refPoint = DocumentManager.Instance.CurrentDBDocument.FamilyCreate.NewReferencePoint(new XYZ());

                trans.Commit();
            }

            var sel = new DSModelElementSelection { SelectedElement = refPoint.Id };

            var buildOutput = sel.BuildOutputAst(new List<AssociativeNode>());

            //function call node builds this
            /*return new IdentifierListNode
            {
                LeftNode = new IdentifierNode(className),
                RightNode = AstFactory.BuildFunctionCall(functionName, arguments)
            };*/

            var funCall = (FunctionCallNode)(((IdentifierListNode)((BinaryExpressionNode)buildOutput.First()).RightNode)).RightNode;
            Assert.IsInstanceOf<IdentifierNode>(funCall.Function);
            Assert.AreEqual(1, funCall.FormalArguments.Count);
            Assert.IsInstanceOf<IntNode>(funCall.FormalArguments[0]);

            Assert.AreEqual(refPoint.Id.IntegerValue, ((IntNode)funCall.FormalArguments[0]).Value);
        }
Example #3
0
        public void CurveByPoints()
        {
            var model = dynSettings.Controller.DynamoModel;

            string samplePath = Path.Combine(_testPath, @".\Curve\CurveByPoints.dyn");
            string testPath = Path.GetFullPath(samplePath);

            Controller.DynamoViewModel.OpenCommand.Execute(testPath);
            Assert.DoesNotThrow(() => dynSettings.Controller.RunExpression());

            //cerate some points and wire them
            //to the selections
            ReferencePoint p1, p2, p3, p4;

            using (_trans = new Transaction(DocumentManager.Instance.CurrentUIDocument.Document))
            {
                _trans.Start("Create reference points for testing.");

                p1 = DocumentManager.Instance.CurrentUIDocument.Document.FamilyCreate.NewReferencePoint(new XYZ(1, 5, 12));
                p2 = DocumentManager.Instance.CurrentUIDocument.Document.FamilyCreate.NewReferencePoint(new XYZ(5, 1, 12));
                p3 = DocumentManager.Instance.CurrentUIDocument.Document.FamilyCreate.NewReferencePoint(new XYZ(12, 1, 5));
                p4 = DocumentManager.Instance.CurrentUIDocument.Document.FamilyCreate.NewReferencePoint(new XYZ(5, 12, 1));

                _trans.Commit();
            }

            var ptSelectNodes = dynSettings.Controller.DynamoModel.Nodes.Where(x => x is DSModelElementSelection);
            if (!ptSelectNodes.Any())
                Assert.Fail("Could not find point selection nodes in dynamo graph.");

            ((DSModelElementSelection)ptSelectNodes.ElementAt(0)).SelectedElement = p1.Id;
            ((DSModelElementSelection)ptSelectNodes.ElementAt(1)).SelectedElement = p2.Id;
            ((DSModelElementSelection)ptSelectNodes.ElementAt(2)).SelectedElement = p3.Id;
            ((DSModelElementSelection)ptSelectNodes.ElementAt(3)).SelectedElement = p4.Id;

            dynSettings.Controller.RunExpression(true);

            FilteredElementCollector fec = new FilteredElementCollector(DocumentManager.Instance.CurrentUIDocument.Document);
            fec.OfClass(typeof(CurveElement));

            Assert.AreEqual(fec.ToElements().Count(), 1);

            CurveByPoints mc = (CurveByPoints)fec.ToElements().ElementAt(0);
            Assert.IsTrue(mc.IsReferenceLine);

            //now flip the switch for creating a reference curve
            var boolNode = dynSettings.Controller.DynamoModel.Nodes.Where(x => x is DSCoreNodesUI.BoolSelector).First();

            ((DSCoreNodesUI.BasicInteractive<bool>)boolNode).Value = false;

            dynSettings.Controller.RunExpression(true);
            Assert.AreEqual(fec.ToElements().Count(), 1);

            mc = (CurveByPoints)fec.ToElements().ElementAt(0);
            Assert.IsFalse(mc.IsReferenceLine);
        }
 public void CancelTransaction()
 {
     if (_trans != null)
     {
         _trans.RollBack();
         _trans = null;
         this.Updater.RollBack(this._transElements);
         this._transElements.Clear();
         this._transDelElements.Clear();
     }
 }
        //private void DrawContainers(Value val, string id)
        //{
        //    if (val.IsList)
        //    {
        //        foreach (Value v in ((Value.List)val).Item)
        //        {
        //            DrawContainers(v, id);
        //        }
        //    }
        //    if (val.IsContainer)
        //    {
        //        var drawable = ((Value.Container)val).Item;

        //        //support drawing XYZs geometry objects or LibG graphic items
        //        if(drawable is XYZ || drawable is GeometryObject || drawable is GraphicItem )
        //        {
        //            VisualizationManager.Visualizations[id].Geometry.Add(drawable);
        //        }
        //    }
        //}

        Value newEval(bool dirty, string script, dynamic bindings, dynamic inputs)
        {
            bool transactionRunning = Transaction != null && Transaction.GetStatus() == TransactionStatus.Started;

            Value result = null;

            if (dynRevitSettings.Controller.InIdleThread)
            {
                result = _oldPyEval(dirty, script, bindings, inputs);
            }
            else
            {
                result = IdlePromise <Value> .ExecuteOnIdle(
                    () => _oldPyEval(dirty, script, bindings, inputs));
            }

            if (transactionRunning)
            {
                if (!IsTransactionActive())
                {
                    InitTransaction();
                }
                else
                {
                    var ts = Transaction.GetStatus();
                    if (ts != TransactionStatus.Started)
                    {
                        if (ts != TransactionStatus.RolledBack)
                        {
                            CancelTransaction();
                        }
                        InitTransaction();
                    }
                }
            }
            else if (DynamoViewModel.RunInDebug)
            {
                if (IsTransactionActive())
                {
                    EndTransaction();
                }
            }

            return(result);
        }
Example #6
0
        /// <summary>
        /// This function gets all the walls in the current Revit document
        /// </summary>
        /// <param name="startNewTransaction">whether do the filtering in a new transaction</param>
        /// <returns>the walls</returns>
        private static IList<Element> GetAllWallElements(bool startNewTransaction)
        {
            if (startNewTransaction)
            {
                using (var trans = new Transaction(DocumentManager.Instance.CurrentUIDocument.Document, "FilteringElements"))
                {
                    trans.Start();

                    ElementClassFilter ef = new ElementClassFilter(typeof(Wall));
                    FilteredElementCollector fec = new FilteredElementCollector(DocumentManager.Instance.CurrentUIDocument.Document);
                    fec.WherePasses(ef);

                    trans.Commit();
                    return fec.ToElements();
                }
            }
            else
            {
                ElementClassFilter ef = new ElementClassFilter(typeof(Wall));
                FilteredElementCollector fec = new FilteredElementCollector(DocumentManager.Instance.CurrentUIDocument.Document);
                fec.WherePasses(ef);
                return fec.ToElements();
            }
        }
        public void InitTransaction()
        {
            if (_trans == null || _trans.GetStatus() != TransactionStatus.Started)
            {
                _trans = new Transaction(
                   dynRevitSettings.Doc.Document,
                   "Dynamo Script"
                );
                _trans.Start();

                FailureHandlingOptions failOpt = _trans.GetFailureHandlingOptions();
                failOpt.SetFailuresPreprocessor(new DynamoWarningPrinter());
                _trans.SetFailureHandlingOptions(failOpt);
            }
        }
Example #8
0
        public void SelectReferenceASTGeneration()
        {
            Form extrude;

            using (var trans = new Transaction(DocumentManager.Instance.CurrentDBDocument, "Create an extrusion Form"))
            {
                trans.Start();

                FailureHandlingOptions fails = trans.GetFailureHandlingOptions();
                fails.SetClearAfterRollback(true);
                trans.SetFailureHandlingOptions(fails);

                var p = new Plane(new XYZ(0, 0, 1), new XYZ());
                var arc = Arc.Create(p, 2, 0, System.Math.PI);
                var sp = SketchPlane.Create(DocumentManager.Instance.CurrentDBDocument, p);
                var mc = DocumentManager.Instance.CurrentDBDocument.FamilyCreate.NewModelCurve(arc, sp);

                var profiles = new ReferenceArray();
                profiles.Append(mc.GeometryCurve.Reference);
                extrude = DocumentManager.Instance.CurrentDBDocument.FamilyCreate.NewExtrusionForm(false, profiles, new XYZ(0,0,1));
                trans.Commit();
            }

            var geom = extrude.get_Geometry(new Options()
            {
                ComputeReferences = true,
                DetailLevel = ViewDetailLevel.Medium,
                IncludeNonVisibleObjects = true
            });

            var solid = geom.FirstOrDefault(x => x is Solid) as Solid;
            var face = solid.Faces.get_Item(0);
            Assert.Greater(solid.Faces.Size, 0);

            var sel = new DSFaceSelection() { SelectedElement = face.Reference };

            var buildOutput = sel.BuildOutputAst(new List<AssociativeNode>());

            var funCall = (FunctionCallNode)((IdentifierListNode)((BinaryExpressionNode)buildOutput.First()).RightNode).RightNode;

            Assert.IsInstanceOf<IdentifierNode>(funCall.Function);
            Assert.AreEqual(1, funCall.FormalArguments.Count);
            Assert.IsInstanceOf<StringNode>(funCall.FormalArguments[0]);

            var stableRef = face.Reference.ConvertToStableRepresentation(DocumentManager.Instance.CurrentDBDocument);
            Assert.AreEqual(stableRef, ((StringNode)funCall.FormalArguments[0]).value);
        }
Example #9
0
        public void CanCreateAndDeleteAReferencePoint()
        {
            using (_trans = _trans = new Transaction(dynRevitSettings.Doc.Document, "CreateAndDeleteAreReferencePoint"))
            {
                _trans.Start();

                FailureHandlingOptions fails = _trans.GetFailureHandlingOptions();
                fails.SetClearAfterRollback(true);
                _trans.SetFailureHandlingOptions(fails);

                ReferencePoint rp = dynRevitSettings.Doc.Document.FamilyCreate.NewReferencePoint(new XYZ());

                //make a filter for reference points.
                ElementClassFilter ef = new ElementClassFilter(typeof(ReferencePoint));
                FilteredElementCollector fec = new FilteredElementCollector(dynRevitSettings.Doc.Document);
                fec.WherePasses(ef);
                Assert.AreEqual(1, fec.ToElements().Count());

                dynRevitSettings.Doc.Document.Delete(rp);
                _trans.Commit();
            }
        }
Example #10
0
        public void CreateInRevitSelectInDynamoSelectDifferentElement()
        {
            //This is to test that when a node is binding with a new element, the information related to binding
            //has actually changed.

            //Create two reference points in Revit
            string rpID1, rpID2;
            ReferencePoint rp1, rp2;
            using (var trans = new Transaction(DocumentManager.Instance.CurrentUIDocument.Document, "CreateInRevit"))
            {
                trans.Start();

                rp1 = DocumentManager.Instance.CurrentUIDocument.Document.FamilyCreate.NewReferencePoint(new XYZ());
                rpID1 = rp1.UniqueId;
                rp2 = DocumentManager.Instance.CurrentUIDocument.Document.FamilyCreate.NewReferencePoint(new XYZ(10, 0, 0));
                rpID2 = rp2.UniqueId;

                trans.Commit();
            }

            //Select the first reference point in Dynamo
            string dynFilePath = Path.Combine(workingDirectory, @".\ElementBinding\SelectInDynamo.dyn");
            string testPath = Path.GetFullPath(dynFilePath);

            ViewModel.OpenCommand.Execute(testPath);

            var model = ViewModel.Model;
            var selNodes = model.AllNodes.Where(x => x is ElementSelection<Autodesk.Revit.DB.Element>);
            var selNode = selNodes.First() as ElementSelection<Autodesk.Revit.DB.Element>;
            IEnumerable<Element> selection1 = new[] { rp1 };
            selNode.UpdateSelection(selection1);
            Assert.DoesNotThrow(() =>ViewModel.Model.RunExpression());
            var id1 = selNode.SelectionResults.First();

            //Select the second reference point in Dynamo
            IEnumerable<Element> selection2 = new[] { rp2 };
            selNode.UpdateSelection(selection2);
            Assert.DoesNotThrow(() =>ViewModel.Model.RunExpression());
            var id2 = selNode.SelectionResults.First();

            //Ensure the element binding is not the same
            Assert.IsTrue(!id1.Equals(id2));
        }
Example #11
0
        public void CreateInRevitSelectInDynamoUndoInRevit()
        {
            //Create a reference point in Revit
            string rpID;
            ReferencePoint rp;
            using (var trans = new Transaction(DocumentManager.Instance.CurrentUIDocument.Document, "CreateInRevit"))
            {
                trans.Start();

                rp = DocumentManager.Instance.CurrentUIDocument.Document.FamilyCreate.NewReferencePoint(new XYZ());
                rpID = rp.UniqueId;

                trans.Commit();
            }

            //Select the reference point in Dynamo
            string dynFilePath = Path.Combine(workingDirectory, @".\ElementBinding\SelectInDynamo.dyn");
            string testPath = Path.GetFullPath(dynFilePath);

            ViewModel.OpenCommand.Execute(testPath);

            var model = ViewModel.Model;
            var selNodes = model.AllNodes.Where(x => x is ElementSelection<Autodesk.Revit.DB.Element>);
            var selNode = selNodes.First() as ElementSelection<Autodesk.Revit.DB.Element>;
            //selNode.SelectionResults.Add(rp);

            Assert.DoesNotThrow(() =>ViewModel.Model.RunExpression());

            //Undo the creation of a reference point in Revit
            Assert.Inconclusive("TO DO");
        }
Example #12
0
        public void CreateInDynamoModifyInRevit()
        {
            //Create a wall in Dynamo
            string dynFilePath = Path.Combine(workingDirectory, @".\ElementBinding\CreateWallInDynamo.dyn");
            string testPath = Path.GetFullPath(dynFilePath);

            ViewModel.OpenCommand.Execute(testPath);
            Assert.DoesNotThrow(() =>ViewModel.Model.RunExpression());

            //Modify the wall in Revit
            using (var trans = new Transaction(DocumentManager.Instance.CurrentUIDocument.Document, "DeleteInRevit"))
            {
                trans.Start();

                IList<Element> rps = GetAllWallElements(false);
                Assert.AreEqual(1, rps.Count);
                Wall wall = rps.First() as Wall;
                //Modify the wall to cause a failure
                Assert.Inconclusive("TO DO");
                wall.Flip();
                DocumentManager.Instance.CurrentDBDocument.Delete(wall);

                trans.Commit();
            }
        }
Example #13
0
        public void CreateInDynamoDeleteInRevit()
        {
            //This test case is to test that elements can be created via Dynamo.
            //After they are deleted in Revit, we can still create them via Dynamo.

            //Create a reference point in Dynamo
            string dynFilePath = Path.Combine(workingDirectory, @".\ElementBinding\CreateInDynamo.dyn");
            string testPath = Path.GetFullPath(dynFilePath);

            ViewModel.OpenCommand.Execute(testPath);
            Assert.DoesNotThrow(() =>ViewModel.Model.RunExpression());
            var model = ViewModel.Model;
            var selNodes = model.AllNodes.Where(x => string.Equals(x.GUID.ToString(), "6a79717b-7438-458a-a725-587be0ba84fd"));
            Assert.IsTrue(selNodes.Any());
            var node = selNodes.First();
            var id1 = GetBindingElementIdForNode(node.GUID);

            //Delete all reference points in Revit
            using (var trans = new Transaction(DocumentManager.Instance.CurrentUIDocument.Document, "DeleteInRevit"))
            {
                trans.Start();

                IList<Element> rps = GetAllReferencePointElements(false);
                var rpIDs = rps.Select(x => x.Id);
                DocumentManager.Instance.CurrentDBDocument.Delete(rpIDs.ToList());

                trans.Commit();
            }

            //Run the graph again
            Assert.DoesNotThrow(() =>ViewModel.Model.RunExpression());
            var id2 = GetBindingElementIdForNode(node.GUID);

            //Check the number of reference points
            //This also verifies MAGN-2317
            IList<Element> newRps = GetAllReferencePointElements(true);
            Assert.AreEqual(1, newRps.Count());

            //Ensure the binding elements are different
            Assert.IsTrue(!id1.Equals(id2));
        }
Example #14
0
        public void XYZFromReferencePointNode()
        {
            DynamoViewModel vm = dynSettings.Controller.DynamoViewModel;

            string samplePath = Path.Combine(_testPath, @".\XYZFromReferencePoint.dyn");
            string testPath = Path.GetFullPath(samplePath);

            dynSettings.Controller.RunCommand(vm.OpenCommand, testPath);
            //dynSettings.Controller.RunCommand(vm.RunExpressionCommand, true);
            ReferencePoint rp;
            using(_trans = new Transaction(dynRevitSettings.Doc.Document))
            {
                _trans.Start("Create a reference point.");

                rp = dynRevitSettings.Doc.Document.FamilyCreate.NewReferencePoint(new XYZ());

                _trans.Commit();

            }
            FSharpList<Value> args = FSharpList<Value>.Empty;
            args = FSharpList<Value>.Cons(Value.NewContainer(rp), args);

            //find the XYZFromReferencePoint node
            var node = dynSettings.Controller.DynamoModel.Nodes.Where(x => x is dynXYZFromReferencePoint).First();

            Value v = ((dynNodeWithOneOutput)node).Evaluate(args);
            Assert.IsInstanceOf(typeof(XYZ), ((Value.Container)v).Item);
        }
Example #15
0
        public void CurveByPointsNode()
        {
            DynamoViewModel vm = dynSettings.Controller.DynamoViewModel;

            string samplePath = Path.Combine(_testPath, @".\CurveByPoints.dyn");
            string testPath = Path.GetFullPath(samplePath);

            dynSettings.Controller.RunCommand(vm.OpenCommand, testPath);

            //cerate some points and wire them
            //to the selections
            ReferencePoint p1, p2, p3, p4;

            using(_trans = new Transaction(dynRevitSettings.Doc.Document))
            {
                _trans.Start("Create reference points for testing.");

                p1 = dynRevitSettings.Doc.Document.FamilyCreate.NewReferencePoint(new XYZ(1, 5, 12));
                p2 = dynRevitSettings.Doc.Document.FamilyCreate.NewReferencePoint(new XYZ(5, 1, 12));
                p3 = dynRevitSettings.Doc.Document.FamilyCreate.NewReferencePoint(new XYZ(12, 1, 5));
                p4 = dynRevitSettings.Doc.Document.FamilyCreate.NewReferencePoint(new XYZ(5, 12, 1));

                _trans.Commit();
            }

            var ptSelectNodes = dynSettings.Controller.DynamoModel.Nodes.Where(x => x is dynPointBySelection);
            if (!ptSelectNodes.Any())
                Assert.Fail("Could not find point selection nodes in dynamo graph.");

            ((dynPointBySelection)ptSelectNodes.ElementAt(0)).SelectedElement = p1;
            ((dynPointBySelection)ptSelectNodes.ElementAt(1)).SelectedElement = p2;
            ((dynPointBySelection)ptSelectNodes.ElementAt(2)).SelectedElement = p3;
            ((dynPointBySelection)ptSelectNodes.ElementAt(3)).SelectedElement = p4;

            dynSettings.Controller.RunCommand(vm.RunExpressionCommand, true);

            FilteredElementCollector fec = new FilteredElementCollector(dynRevitSettings.Doc.Document);
            fec.OfClass(typeof(CurveElement));

            Assert.AreEqual(fec.ToElements().Count(), 1);

            CurveByPoints mc = (CurveByPoints)fec.ToElements().ElementAt(0);
            Assert.IsTrue(mc.IsReferenceLine);

            //now flip the switch for creating a reference curve
            var boolNode = dynSettings.Controller.DynamoModel.Nodes.Where(x => x is dynBoolSelector).First();

            ((dynBasicInteractive<bool>)boolNode).Value = false;

            dynSettings.Controller.RunCommand(vm.RunExpressionCommand, true);
            Assert.AreEqual(fec.ToElements().Count(), 1);

            mc = (CurveByPoints)fec.ToElements().ElementAt(0);
            Assert.IsFalse(mc.IsReferenceLine);
        }
 public void EndTransaction()
 {
     if (_trans != null)
     {
         if (_trans.GetStatus() == TransactionStatus.Started)
         {
             _trans.Commit();
             _transElements.Clear();
             CommitDeletions();
             _transDelElements.Clear();
         }
         _trans = null;
     }
 }
Example #17
0
 public TransactionProcedureNode(Transaction node, IEnumerable<string> inputNames)
     : base(inputNames)
 {
     _node = node;
 }
Example #18
0
        /// <summary>
        /// Creates two model curves separated in Z.
        /// </summary>
        /// <param name="mc1"></param>
        /// <param name="mc2"></param>
        private void CreateTwoModelCurves(out ModelCurve mc1, out ModelCurve mc2)
        {
            //create two model curves
            using (_trans = _trans = new Transaction(dynRevitSettings.Doc.Document, "CreateTwoModelCurves"))
            {
                _trans.Start();

                Plane p1 = new Plane(XYZ.BasisZ, XYZ.Zero);
                Plane p2 = new Plane(XYZ.BasisZ, new XYZ(0, 0, 5));

                SketchPlane sp1 = dynRevitSettings.Doc.Document.FamilyCreate.NewSketchPlane(p1);
                SketchPlane sp2 = dynRevitSettings.Doc.Document.FamilyCreate.NewSketchPlane(p2);
                Curve c1 = dynRevitSettings.Revit.Application.Create.NewLineBound(XYZ.Zero, new XYZ(1, 0, 0));
                Curve c2 = dynRevitSettings.Revit.Application.Create.NewLineBound(new XYZ(0, 0, 5), new XYZ(1, 0, 5));
                mc1 = dynRevitSettings.Doc.Document.FamilyCreate.NewModelCurve(c1, sp1);
                mc2 = dynRevitSettings.Doc.Document.FamilyCreate.NewModelCurve(c2, sp2);

                _trans.Commit();
            }
        }