Wrapping api element to dynamo element

Wrapping api element to dynamo element

ameer.mansourWAK8Y
Advocate Advocate
4,230 Views
9 Replies
Message 1 of 10

Wrapping api element to dynamo element

ameer.mansourWAK8Y
Advocate
Advocate

For each element two different namespace . i.e. levels
In Revit's API, the Level class' fully-qualified name is: Autodesk.Revit.DB.Level
In Dynamo, the Level class' fully-qualified name is: Revit.Elements.Level

 

I am creating a Revit API add-in using c# . I want to select an element but wrap it to dynamo namespace.

is that possible?

Accepted solutions (1)
4,231 Views
9 Replies
Replies (9)
Message 2 of 10

jeremy_tammik
Alumni
Alumni

Of course it is possible.

 

I cannot tell you how off-hand myself, I'm afraid, but maybe some other Revit API discussion forum participant can.

 

Alternatively, the entire Dynamo framework is open source, proving (a) that it is possible and (b) giving you access to the full implementation source code:

 

https://github.com/DynamoDS

 

So, you might be able to find out how yourself from the Dynamo source code, or you could ask the Dynamo experts in the Dynamo discussion forum:

  

https://forum.dynamobim.com

  

Also, The Building Coder article on a Dynamo zero touch node Revit element wrapper might help:

  

https://thebuildingcoder.typepad.com/blog/2019/08/zero-touch-node-element-wrapper-and-load-from-stre...

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 10

ameer.mansourWAK8Y
Advocate
Advocate
Accepted solution

hi Jeremy,

i found a solution for that. There is unwrap Method that can be located in 
C:\Program Files\Autodesk\Revit 2021\AddIns\DynamoForRevit\Revit\RevitNodes.dll

under namespace: Revit.Elements
a public static class named: ElementWrapper
it contains all the needed wrappers.

ameermansourWAK8Y_0-1632820860335.png

 

Message 4 of 10

ameer.mansourWAK8Y
Advocate
Advocate

@jeremy_tammik 
sorry to bother you.
I still get this error

ameermansourWAK8Y_0-1632832748992.png



and i think i need to initialize documentManager instance 

ameermansourWAK8Y_1-1632832769876.png



am i right? or do you have any idea about what should i be looking for?

0 Likes
Message 5 of 10

jeremy_tammik
Alumni
Alumni

Nope, sorry, no idea. I have never worked with this myself. I would suggest discussing this in the Dynamo forum, unless someone else from here can chip in and help.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 6 of 10

ameer.mansourWAK8Y
Advocate
Advocate

after digging 6 hours into this problem. I was able to make it work.
it needed to setup an environment like a dynamo environment.

here is my code sample in case anyone needed it.

 

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {

            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Application app = uiapp.Application;
            Document doc = uidoc.Document;


 #region Core Logic

                       IList<Reference> refElementsA;
           
            try
            {
                refElementsA = uidoc.Selection.PickObjects(ObjectType.Element, "Please pick elements (set A)");
            }
            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                return Result.Cancelled;
            }

            
// this is must-do assignmet so that we can use wrapper functions
            DocumentManager.Instance.CurrentUIDocument = uidoc;
            List<Revit.Elements.Element> elementSetA = Helper.GetWrappedElements(refElementsA, doc);


            #endregion
//this is must do call as wrapper function opens an transaction but doesn't close it.
//if not called an error will be shown to the user after excuting the command.
            TransactionManager.Instance.ForceCloseTransaction();
            return Result.Succeeded;

}

        /// <summary>
        /// this is our helper method DocumentManager.Instance.CurrentUIDocument = uidoc must be setted before calling this function.
        /// </summary>
        /// <param name="refElements"></param>
        /// <param name="mainDoc"></param>
        /// <returns></returns>
        public static List<Revit.Elements.Element> GetWrappedElements(IList<Reference> refElements,Document mainDoc)
        {
            List<Revit.Elements.Element> result = new List<Revit.Elements.Element>();
            foreach (var refElement in refElements)
            {
                
                    
                    var rvtElement = mainDoc.GetElement(refElement);
                    result.Add(Revit.Elements.ElementWrapper.Wrap(rvtElement, true));
               
            }

            return result;
        }

 

Message 7 of 10

tomax1029
Explorer
Explorer

I'm sorry, but could you help me? I'm doing the same thing, but in Python, here's my code. And all this thing ends with error

# -*- coding: utf8 -*-
import clr

clr.AddReference('ProtoGeometry')
import Autodesk.DesignScript.Geometry

# Import Element wrapper extension methods
clr.AddReference("RevitNodes")
import Revit

clr.ImportExtensions(Revit.Elements)

# Import geometry conversion extension methods
clr.ImportExtensions(Revit.GeometryConversion)

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

doc = __revit__.ActiveUIDocument.Document

elements = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_PipeCurves).WhereElementIsNotElementType().ToElements()

for i in elements:
    print(Revit.Elements.Element.ElementWrapper.Wrap(i, True))

 

tomax1029_0-1648409727868.png

 

0 Likes
Message 8 of 10

jeremy_tammik
Alumni
Alumni

Your code includes a call to `Elements.Element.ElementWrapper`.

 

The error message clearly states: 'type' object has no attribute 'ElementWrapper'.

 

`Element` is a Revit API class, i.e., a type in Python wording.

 

So, you cannot call ElementWrapper on the Element class.

   

Please note that this forum is mainly dedicated to the pure Revit API, not Pythons specific issues.

  

You may possibly find more help and expertise on the latter in the Dynamo forum:

 

https://forum.dynamobim.com

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 9 of 10

danail.momchilov
Contributor
Contributor

Thank you for your post @ameer.mansourWAK8Y !

 

I've been looking for the location of the RevitNodes reference for Dynamo 2.0 and beyond. There was no information on the web about its new location, so I'm glad you found it! Here's a simple implementation for anyone who might find it useful:

// load Revit api
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

// load Dynamo API (after adding the RevitNodes reference)
using Revit.Elements;

namespace Sample
{
    public class Sample
    {
        public static Autodesk.Revit.DB.Element ReturnUnwrapped(Revit.Elements.Element element)
        {
            try
            {
                Autodesk.Revit.DB.Element UnwrappedElement = element.InternalElement;
                return UnwrappedElement;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}

 

0 Likes
Message 10 of 10

danail.momchilov
Contributor
Contributor

I see this is really old post, but it seems like you are making a script for pyRevit... You don't need to Wrap and Unwrap anything when accessing the API through pyRevit, you can just work with the api elements directly

0 Likes