Message 1 of 16
Having a problem creating room tags from Linked model
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi all! Thanks in advance for any help you can provide as I'm stuck on getting the tags to show. I've got this working in Dynamo and I'm not sure why I'm having an issue here in c#:
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
//private IList<ElementId> roomids = new IList(ElementId);
public List<ElementId> roomids = new List<ElementId>();
//public List<LinkElementId> roomLids = new List<LinkElementId>();
public ElementId linkDocId = new ElementId(BuiltInCategory.OST_RvtLinks);
//public FamilySymbol tagType = new FamilySymbol();
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;
Transaction trans = new Transaction(doc);
trans.Start("Create Room Tags");
Document linkDoc = GetLinkRoomElementIds(uiapp, doc);
TaskDialog.Show("ROOM COUNT", roomids.Count().ToString());
View v = doc.ActiveView as View;
//CreateRoomTags(linkDoc, v);
CreateRoomTags(linkDoc, doc, v);
doc.Regenerate();
trans.Commit();
return Result.Succeeded;
}
private void CreateRoomTags(Document link, Document doc, View v)
{
for (int i = 0; i < roomids.Count; i++)
{
ElementId roomid = roomids[i];
LinkElementId roomLids = new LinkElementId(roomid);
Element e = link.GetElement(roomid);
Room r = e as Room;
if (r != null)
{
XYZ cen = GetRoomCenter(r);
if (cen != null)
{
UV center = new UV(cen.X, cen.Y);
var roomTag = doc.Create.NewRoomTag(roomLids,
center,
v.Id);
roomTag.ChangeTypeId(MSARoomTagID(roomTag, doc));
}
else i++;
}
else i++;
//RoomTag roomTag = doc.Create.NewRoomTag(new LinkElementId(linkDocId, roomid),
// center,
// v.Id);
//roomTag.RoomTagType(tagType);
}
}
public ElementId MSARoomTagID(RoomTag tag, Document doc)
{
string newTagTypeName = "Room Tag";
string newTagFamilyName = "MSA-G-Room Tag";
ElementId newTagTypeId = null;
foreach (ElementId id in tag.GetValidTypes())
{
RoomTagType type = doc.GetElement(id) as RoomTagType;
if (type.Name.Equals(newTagTypeName) &&
type.FamilyName.Equals(newTagFamilyName))
{
newTagTypeId = id;
return newTagTypeId;
}
}
return null;
}
public XYZ GetRoomCenter(Room room)
{
// Get the room center point.
XYZ boundCenter = GetElementCenter(room);
if (boundCenter != null)
{
LocationPoint locPt = (LocationPoint)room.Location;
XYZ roomCenter = new XYZ(boundCenter.X, boundCenter.Y, locPt.Point.Z);
return roomCenter;
}
return null;
}
public XYZ GetElementCenter(Element elem)
{
if (elem != null)
{
BoundingBoxXYZ bounding = elem.get_BoundingBox(null);
try
{
XYZ center = (bounding.Max + bounding.Min) * 0.5;
return center;
}
catch (Exception)
{
return null;
}
}
else return null;
}
public Document GetLinkRoomElementIds(UIApplication uiapp, Document doc)
{
FilteredElementCollector links = new FilteredElementCollector(doc);
IList<Element> linkElems = links.OfCategory(BuiltInCategory.OST_RvtLinks).OfClass(typeof(RevitLinkType)).ToElements();
for (int i = 0; i < linkElems.Count; i++)
{
Element e = linkElems[i];
RevitLinkType linkType = e as RevitLinkType;
String linkName = String.Concat(linkType.Name.Reverse().Skip(4).Reverse());
foreach (Document linkedDoc in uiapp.Application.Documents)
{
if (linkedDoc.Title.Equals(linkName))
{
linkDocId = e.Id;
FilteredElementCollector collLinked = new FilteredElementCollector(linkedDoc);
IList<Element> linkedRooms = collLinked
.OfClass(typeof(SpatialElement)).ToElements();
if (linkedRooms != null)
{
foreach (Element l in linkedRooms)
{
roomids.Add(l.Id);
}
}
return linkedDoc;
}
}
}
return null;
}
}
I'm sure Im missing something obvious here, thanks again!
