Hi @ddk0705 ,
You can get the tag associated with the room
Transaction actrans = new Transaction(doc);
actrans.Start("transaction name");
Room room;
IList<ElementId> eids = doc.Delete(room.Id) as IList<ElementId>;
actrans.RollBack();
Now eids contain room and tag.
From the list get the room tag
RoomTag RT = null;
foreach(ElementId eid1 in eids)
{
Element e = doc.GetElement(eid1);
if(e.Name.Contains("Tag"))
{
RT = e as RoomTag;
}
}
Also, check this below link
@naveen.kumar.t Thanks for your reply. How can I get the bounding box of room tag?
Hi @ddk0705 ,
I didn't check the question properly. Here is the code to get the room tag of the Room from the linked document
1)first get the linked document
//get the linked document
Document d = null;
foreach(Document docc in app.Documents)
{
if(docc.IsLinked && docc.Title=="YOUR LINKED DOCUMENT NAME")
{
d = docc;
}
}
2)get the room from the linked document
//select the element(ROOM) in linked document
Reference R = uidoc.Selection.PickObject(ObjectType.LinkedElement);
ElementId eid = R.LinkedElementId;
Element e = d.GetElement(eid);
Room roomroom = e as Room;
3)get the Required Room tag and get the bounding box of the room tag
RoomTag requiredRoomTag = null;
if(roomroom!=null)
{
FilteredElementCollector coll = new FilteredElementCollector(d).OfCategory(BuiltInCategory.OST_RoomTags).WhereElementIsNotElementType();
foreach(Element ee in coll)
{
RoomTag RT = ee as RoomTag;
Room r = RT.Room;
if(r.Name==roomroom.Name)
{
//Required roomtag
requiredRoomTag = RT;
BoundingBoxXYZ bBox = requiredRoomTag.get_BoundingBox(doc.ActiveView);
if(bBox!=null)
{
TaskDialog.Show("YES", "BOUNDING BOX IS PRESENT");
}
}
}
}
I hope this helps.
@naveen.kumar.t Thanks for your feedback!
But I've got the room tag by current document, not linked document.
I've tried to get the bounding box of tag by your solution but the result is null.
So I'm trying to move the tag to another position. Ex: room center
But I've got some errors
Could you give me the solution for this case?
Thanks,
Here is the code that I use to center the Room Tags in the current View.
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using System;
using System.Windows.Forms;
namespace RDGRevit.Commands
{
[Transaction(TransactionMode.Manual)]
class CenterViewRoomTags : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiapp = commandData.Application;
Document doc = uiapp.ActiveUIDocument.Document;
try
{
using (Transaction t = new Transaction(doc))
{
t.Start("Center Room Tags");
foreach (RoomTag roomTag in new FilteredElementCollector(doc,doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_RoomTags).WhereElementIsNotElementType().ToElements())
{
if (!roomTag.IsTaggingLink)
{
Room room = doc.GetElement(roomTag.TaggedLocalRoomId) as Room;
LocationPoint rlp = room.Location as LocationPoint;
LocationPoint rtlp = roomTag.Location as LocationPoint;
XYZ p1 = rlp.Point;
XYZ p2 = rtlp.Point;
if (room.IsPointInRoom(p1))
{
ElementTransformUtils.MoveElement(doc, roomTag.Id, p1 - p2);
}
}
else
{
RevitLinkInstance link = doc.GetElement(roomTag.TaggedRoomId.LinkInstanceId) as RevitLinkInstance;
Document linkDoc = link.GetLinkDocument();
Room linkRoom = linkDoc.GetElement(roomTag.TaggedRoomId.LinkedElementId) as Room;
LocationPoint rlp = linkRoom.Location as LocationPoint;
LocationPoint rtlp = roomTag.Location as LocationPoint;
XYZ p1 = rlp.Point;
XYZ p2 = rtlp.Point;
if (linkRoom.IsPointInRoom(p1))
{
ElementTransformUtils.MoveElement(doc, roomTag.Id, p1 - p2);
}
}
}
t.Commit();
}
return Result.Succeeded;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return Result.Failed;
}
}
}
}
@Sean_Page Thanks for your feedback!
I've tried your solution. I've thought it's good for my case.
But, It seems not effective. This is my logic code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using RoomLabel.DAO;
using RTM.Helpers.Common;
namespace RoomLabel.CustomExternalEvent
{
class ExternalEventMoving : IExternalEventHandler
{
private StoreSingleton _storeSingleton;
public void Execute(UIApplication app)
{
try
{
UIDocument uidoc = app.ActiveUIDocument;
_storeSingleton = StoreSingleton.Instance;
using (Transaction t = new Transaction(uidoc.Document))
{
t.Start("Moving label");
List<RoomInfo> roomInfo = _storeSingleton.RoomInfo;
if (roomInfo.Count > 0)
{
foreach (RoomInfo rie in roomInfo)
{
Room room = rie.Room;
RoomTag roomTag = rie.RoomTag;
XYZ centerRoom = RoomDAO.GetRoomCenter(room);
BoundingBoxXYZ bbox = roomTag.get_BoundingBox(uidoc.Document.ActiveView);
CenterRoomTags(uidoc.Document, roomTag);
}
}
t.Commit();
}
}
catch (Exception ex)
{
MessageBox.Show(Logger.CreateMsg("ExternalEventMoving.cs", "Execute", ex.Message));
}
}
public void CenterRoomTags(Document doc, RoomTag roomTag)
{
RevitLinkInstance link = doc.GetElement(roomTag.TaggedRoomId.LinkInstanceId) as RevitLinkInstance;
Document linkDoc = link.GetLinkDocument();
Room linkRoom = linkDoc.GetElement(roomTag.TaggedRoomId.LinkedElementId) as Room;
LocationPoint rlp = linkRoom.Location as LocationPoint;
LocationPoint rtlp = roomTag.Location as LocationPoint;
XYZ p1 = rlp.Point;
XYZ p2 = rtlp.Point;
if (linkRoom.IsPointInRoom(p1))
{
ElementTransformUtils.MoveElement(doc, roomTag.Id, p1 - p2);
}
}
public string GetName()
{
return "ExternalEventMoving";
}
}
}
Could you send me a solution for this case?
Thanks,
@ddk0705could you tell me what doesn't work for you? All my code does is move the Room tag to the Room Location point. This code does not "center" the Room Tag or the Room, but from the code you posted I seemed like you already had a method to do that.
I assume the Room Location Point is within the Room Boundary? I have used my code many times so I know it works as expected. Can you put a couple TaskDialogs in the mix to see if you are getting Values / Room / Tags as expected?
I can tell that the parameters have values from your break point, however I cannot tell where the Room location point is (where the tag is supposed to be going).
Here is an example of my code running in a project. Is this what you are looking for?
@Sean_Page Thanks,
I've put the break point, so I've seen that the location of tag room had changed.
No way! The tag room doesn't move on the sheet.
ElementTransformUtils.MoveElement
I think that we can't create a transaction for elements of linked document.
What do you think about it?
I think I may have misunderstood what you were looking for. You will not be able to move Tags IN a linked model no, but I thought you were trying to move Tags in a model based on the linked rooms. That was what my solution showed.
@Sean_Page Thanks for your help!