Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Original Type from Revit Part

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
augustinoj
695 Views, 4 Replies

Original Type from Revit Part

I'm fairly new to the Revit APIs and I'm trying figure out how to get the Original Type from a Part object.  Heres' what I have so far.  I can get the Original Category but I can't figure out how to get the Original Type.

 

            Autodesk.Revit.UI.UIApplication m_revit = revit;
            var allDocs = new List<Document> { m_revit.ActiveUIDocument.Document };
            IList<ElementFilter> filters = new List<ElementFilter>(1);
            filters.Add(new ElementClassFilter(typeof(Part)));
            LogicalOrFilter orFilter = new LogicalOrFilter(filters);

            FilteredElementCollector parts = new FilteredElementCollector(m_revit.ActiveUIDocument.Document)
                                                .OfClass(typeof(Part));
            FilteredElementIterator iterator = parts.WherePasses(orFilter).GetElementIterator();
            int nKnt = 0;
            while (iterator.MoveNext())
            {
                nKnt++;
                Part pPart = (iterator.Current) as Part;
                if (pPart != null)
                {
                    Category pOrig = Category.GetCategory(m_revit.ActiveUIDocument.Document, pPart.OriginalCategoryId);

                    MessageBox.Show(nKnt.ToString() + " = " + pPart.Category.Name + ", " + pOrig.Name);


                }
            }

Thanks,

Joe

4 REPLIES 4
Message 2 of 5
jeremytammik
in reply to: augustinoj

Dear Joe,

 

The short answer is:

 

Element.GetTypeId();

 

A longer and older answer is provided here:

 

http://thebuildingcoder.typepad.com/blog/2010/08/get-element-type.html

 

I would very strongly recommend a much more sustainable approach, though:

 

Please work through the getting started material first of all, especially the DevTV and My first Revit plugin tutorials:

 

http://thebuildingcoder.typepad.com/blog/about-the-author.html#2

 

They will answer this question and many others as well in depth, further your understanding, and save both you and your peers lots of future effort and discussion.

 

I hope this helps.

 

Best regards,

 

Jeremy



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

Message 3 of 5
augustinoj
in reply to: jeremytammik

Ok, I added this after my MessageBox.Show() call but I get "Object reference not set to an instance of an object" error.

                    ElementId id = pPart.GetTypeId();
                    ElementType type = m_revit.ActiveUIDocument.Document.GetElement(id) as ElementType;
                    string AssemblyCode = type.get_Parameter(BuiltInParameter.UNIFORMAT_CODE).AsString();
                    MessageBox.Show("  Type = " + AssemblyCode);

 

I will go through tutorials, thanks for the link, but I do need to get this portion of code that I inherited up and running.

 

Thanks,

Joe

Message 4 of 5
jeremytammik
in reply to: augustinoj

Dear Joe,

 

Please confirm that your form is modeless, otherwise all further discussion is completely pointless.

 

If so, you are not in a valid Revit API context.

 

You can create one for yourself by raising an external event.

 

You need to re-architect your add-in to use external events.

 

Read the discussions that I pointed in my last message:

 

http://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28

 

Then you will understand.

 

Alternatively, e.g., if you are averse to reading, you can simply jump straight to the appropriate Revit SDK sample, ModelessDialog/ModelessForm_ExternalEvent, and work it out from there yourself.

 

It demonstrates best practices accessing the Revit API from a modeless form via an external event.

 

Base your application on that and you can continue to avoid all additional reading.

 

I hope this clarifies.

 

Cheers,

 

Jeremy



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

Message 5 of 5
augustinoj
in reply to: jeremytammik

I figured out the solution:

 

    [Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        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;

            FilteredElementCollector col = new FilteredElementCollector(doc)
                .WhereElementIsNotElementType()
                .OfCategory(BuiltInCategory.OST_Parts)
                .OfClass(typeof(Part));

            // Filtered element collector is iterable

            foreach (Element e in col)
            {
                Part pPart = e as Part;
                ICollection<LinkElementId> pLinkId = pPart.GetSourceElementIds();
                foreach (LinkElementId pId in pLinkId)
                {
                    Element pEl;
                    if (pId.HostElementId.IntegerValue != -1)
                    {
                        pEl = doc.GetElement(pId.HostElementId);
                    }
                    else if (pId.LinkedElementId.IntegerValue != -1)
                    {
                        pEl = doc.GetElement(pId.LinkedElementId);
                    } else {
                        continue;
                    }
                    if (pEl != null)
                    {
                        ElementId pTypeId = pEl.GetTypeId();
                        ElementType pOrigTyp = doc.GetElement(pTypeId) as ElementType;
                        Debug.Print("Category=" + pEl.Category.Name + ", Family=" + pOrigTyp.FamilyName + ", Type = " + pEl.Name);
                    }
                    ;
                }
                //Debug.Print(e.Name);
            }

            return Result.Succeeded;
        }
    }

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community