New ZeroTouch foray Dynamo - cant get active document

New ZeroTouch foray Dynamo - cant get active document

thomas
Advocate Advocate
2,028 Views
4 Replies
Message 1 of 5

New ZeroTouch foray Dynamo - cant get active document

thomas
Advocate
Advocate

Hi, 

 

I'm attempting to port one of my Python nodes from Dynamo to ZeroTouch and I cant get the active document. All I am trying to do is convert the Python code to C# then import the library into Dynamo, not exactly hard, only I dont know how to get the active doc. I understand I have to create an IExternalCommand object then call an execute function and that's where I'm stumped as I cant return the document object from it. Can someone point me in the right direction:

 

Original code in Python:

 

 

import clr

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager

# Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

doc = DocumentManager.Instance.CurrentDBDocument

lineStyle = doc.Settings.Categories.get_Item( BuiltInCategory.OST_Lines )
lineStyleSubTypes = lineStyle.SubCategories

listNames = []
listRGB = []
lineWeight = []
for i in lineStyleSubTypes:
	rgb = [i.LineColor.Red, i.LineColor.Green, i.LineColor.Blue]
	weight = i.GetLineWeight(GraphicsStyleType.Projection)
	
	listNames.append(i.Name)
	listRGB.append(rgb)
	lineWeight.append(weight)

OUT = listNames, lineWeight, listRGB

 

 

Attempts in C#

 

public class GetActiveDocument : IExternalCommand
    {
        private Document _doc;

        internal GetActiveDocument(Document doc)
        {
            _doc = doc;

        }

        public static GetActiveDocument getDoc2(Document doc)
        {
            return new GetActiveDocument(doc);
        }

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //Get application and document objects
            UIApplication uiApp = commandData.Application;
            Document doc = uiApp.ActiveUIDocument.Document;

            return Result.Succeeded;
        }

        public Document getDoc
        {
            get { return _doc; }
        }
    }

 

0 Likes
Accepted solutions (1)
2,029 Views
4 Replies
Replies (4)
Message 2 of 5

jeremytammik
Autodesk
Autodesk
Accepted solution

Dear Thomas,

 

Sorry, I cannot help you much, except to say that this approach will not work just like that, since the Document instance that you have access to within the Execute method is only valid until that method returns.

 

If you pass it out of the Execute method somewhere else, you will no longer be inside the valid Revit API context, and will not be able to make use pf the Document instance that you passed out of there.

 

Sorry for the bad news.

 

Merry Christmas!

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 5

thomas
Advocate
Advocate

Hi Jeremy 

 

Thanks for the quick response, its helped kick start my attempts to resolve it and I realise the solution was staring me in the face. Dynamo for Revit has its own RevitServices library (I import it in that snippet of Python I posted!). I simply reference this dll into my project and port the code and all works as expected. Cheers for the insights, highly appreciated and Merry Xmas to you too:

 

public class LineStyles
    {
        public static List<string> GetLineStyles()
        {
            Document doc = RevitServices.Persistence.DocumentManager.Instance.CurrentDBDocument;

            // Get the handle of current document.

            Category c = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);

            CategoryNameMap subcats = c.SubCategories;

            List<string> listNames = new List<string>();
            foreach (Category lineStyle in subcats)
            {
                listNames.Add(lineStyle.Name);
            };
            return listNames;
        }
    }

Capture.JPG

 

Message 4 of 5

tom_james
Contributor
Contributor

For others trying to implement this. I had to add the "RevitServices.dll", which is not normally added to Revit only API calls.

 

I found this at:

"C:\Program Files\Dynamo\Dynamo Revit\"..Dynamo version..."\Revit_"...Revit version..."\RevitServices.dll"

 

E.g.

"C:\Program Files\Dynamo\Dynamo Revit\2\Revit_2019\RevitServices.dll"

0 Likes
Message 5 of 5

tom_james
Contributor
Contributor

This has now moved to:

"C:\Program Files\Autodesk\Revit_"...Revit version..."\AddIns\DynamoForRevit\Revit\RevitServices.dll"

 

E.g.:

"C:\Program Files\Autodesk\Revit 2021\AddIns\DynamoForRevit\Revit\RevitServices.dll"

 

0 Likes