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
Solved! Go to Solution.
Solved by augustinoj. Go to Solution.
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
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
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
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; } }