Tag link spaces

Tag link spaces

H.echeva
Advocate Advocate
576 Views
4 Replies
Message 1 of 5

Tag link spaces

H.echeva
Advocate
Advocate

Hello,

I am trying to use the Revit API to tag spaces in a linked model.

I see that the  CreateNewRoomTagmethod is easier because you can use the LinkElementId class.

However, this is not allowed with the CreateNewSpaceTag method:

public SpaceTag NewSpaceTag(
	Space space,
	UV point,
	View view
)

The first argument is Space, and I don't see any way of getting the space from the linked model this way.

I have tried something like:

doc.GetElement(myLinkElementId.LinkedElementId) as Space (I have also tried myLinkElementId.HostElementId)

But this is not working.

 

Here is my attempt:

 

public void RDSTag()
		{
			Document doc = this.ActiveUIDocument.Document;
			FilteredElementCollector linkInstanceCollector = new FilteredElementCollector(doc).OfClass(typeof( RevitLinkInstance));
						
			RevitLinkInstance myLink = null;
			
			//get link instance with desired name
			foreach(RevitLinkInstance r in linkInstanceCollector)
			{
				if(r.Name.Contains("HRL")) myLink = r;
			}
			
			//get link's document
			Document linkDoc = myLink.GetLinkDocument();
			
			// Get linkType
		    RevitLinkType linkType = doc.GetElement(myLink.GetTypeId() ) as RevitLinkType;
		    
		     // Check if link is loadedmk
		    if( RevitLinkType.IsLoaded( doc, linkType.Id ) )
		    {
		      // Find spaces for tagging
		      ICollection<ElementId> spaceCollector = new FilteredElementCollector(linkDoc).OfClass(typeof(SpatialElement)).WhereElementIsNotElementType().ToElementIds();
//		      string spaceIds = "";
		      
		      using(Transaction t = new Transaction(doc,"tag Spaces"))
		      {
		      	
		      	t.Start();
		      	string message = "";
		      	foreach(ElementId eid in spaceCollector)
		      	{
		      		LinkElementId linkedSpaceReferenceId = new LinkElementId(myLink.Id, eid);

		      		Space space = doc.GetElement(linkedSpaceReferenceId.LinkedElementId) as Space;
			      	doc.Create.NewSpaceTag(space, new UV(0, 0), doc.ActiveView);
		      	}
		      
		      	t.Commit();
		      }

 

Any help is welcome, thanks

 

0 Likes
Accepted solutions (1)
577 Views
4 Replies
Replies (4)
Message 2 of 5

jeremy_tammik
Alumni
Alumni

Please ensure that the `doc` variable in your expression `doc.GetElement(myLinkElementId.LinkedElementId)` must refer to the linked document containing the element that your are trying to access, e.g., the space you are trying to tag. Here is an article on taggin linked elements:

 

https://thebuildingcoder.typepad.com/blog/2019/05/tagging-a-linked-element.html

  

 

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 3 of 5

H.echeva
Advocate
Advocate

Hello Jeremy,

Thank you very much for your reply.

I had read your document and tried to apply it to my case before posting here. However, if I am not mistaken, in your example you use References, which work with the IndepentTag method but not with the NewSpaceTag method.

 

I have changed the code to what you suggested:

foreach(ElementId eid in spaceCollector)
		      	{
		      		
		      			LinkElementId linkedSpaceReferenceId = new LinkElementId(myLink.Id, eid);

			      		Space space = linkDoc.GetElement(linkedSpaceReferenceId.LinkedElementId) as Space;

			      		message += space.Name + Environment.NewLine;
			      		
			      		//if I comment the line bellow, the Taskdialog shows all the space names
				      	doc.Create.NewSpaceTag(space, new UV(0, 0), doc.ActiveView);
		      	}
	      		TaskDialog.Show("test",message);

 

But I am getting the following error:

Hecheva_0-1633778851391.png

As you can see I have put a TaskDialog that successfully returns the name of the spaces if I comment the line as per the comment.

 

 

 

0 Likes
Message 4 of 5

jeremy_tammik
Alumni
Alumni
Accepted solution

`space` lives in `linkDoc`, and apparently `NewSpaceTag` needs to operate on `linkDoc` as well to work successfully.

 

So, it appears that your approach is not possible.

 

You should always research for a viable approach manually through the user interface before trying to address it programmatically.

 

Please read more about how to research to find a Revit API solution:

 

https://thebuildingcoder.typepad.com/blog/2017/01/virtues-of-reproduction-research-mep-settings-onto...

 

Once you have successfully sorted out an optimal manual workflow, you can proceed to implement the automated API solution implementing it.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 5 of 5

H.echeva
Advocate
Advocate

Thank you very much again for your help and for your article. I will read it carefully.

0 Likes