Get room tag

Get room tag

ddk0705
Contributor Contributor
1,952 Views
17 Replies
Message 1 of 18

Get room tag

ddk0705
Contributor
Contributor

Hi guys,

How can I get the room tag by the Room of linked document?

Thanks,

0 Likes
1,953 Views
17 Replies
Replies (17)
Message 2 of 18

naveen.kumar.t
Autodesk Support
Autodesk Support

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;
       }
   }

 


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 18

naveen.kumar.t
Autodesk Support
Autodesk Support

Also, check this below link

https://forums.autodesk.com/t5/revit-api-forum/i-want-to-check-whether-tag-is-present-on-door-by-api... 


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 4 of 18

Sean_Page
Collaborator
Collaborator

Since the OP asked about a Linked Room I don't believe the temporary Transaction method would be effective in this situation because you can't modify nor start a transaction in a linked Document.

Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
0 Likes
Message 5 of 18

ddk0705
Contributor
Contributor

@naveen.kumar.t Thanks for your reply. How can I get the bounding box of room tag?

0 Likes
Message 6 of 18

naveen.kumar.t
Autodesk Support
Autodesk Support

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
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 7 of 18

ddk0705
Contributor
Contributor

@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

1.png2.png3.png

Could you give me the solution for this case? 

Thanks,

0 Likes
Message 8 of 18

Sean_Page
Collaborator
Collaborator

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, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
Message 9 of 18

ddk0705
Contributor
Contributor

@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,

0 Likes
Message 10 of 18

Sean_Page
Collaborator
Collaborator

@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.

Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
0 Likes
Message 11 of 18

ddk0705
Contributor
Contributor

@Sean_Page The room tag didn't move after the Transaction had done successfully.

 

 

0 Likes
Message 12 of 18

Sean_Page
Collaborator
Collaborator

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?

Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
0 Likes
Message 13 of 18

ddk0705
Contributor
Contributor
0 Likes
Message 14 of 18

Sean_Page
Collaborator
Collaborator

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, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
0 Likes
Message 15 of 18

ddk0705
Contributor
Contributor

@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.

0 Likes
Message 16 of 18

ddk0705
Contributor
Contributor

@Sean_Page 

ElementTransformUtils.MoveElement

I think that we can't create a transaction for elements of linked document.

What do you think about it?

0 Likes
Message 17 of 18

Sean_Page
Collaborator
Collaborator

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, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
0 Likes
Message 18 of 18

ddk0705
Contributor
Contributor

@Sean_Page Thanks for your help!

0 Likes