Revit ElementTransformUtils.CopyElements returns ids of null elements ?

Revit ElementTransformUtils.CopyElements returns ids of null elements ?

Anonymous
Not applicable
1,548 Views
8 Replies
Message 1 of 9

Revit ElementTransformUtils.CopyElements returns ids of null elements ?

Anonymous
Not applicable

I am using the Autodesk.Revit.DB.ElementTransformUtils.CopyElements method defined here:

https://www.revitapidocs.com/2016/b22df8f6-3fa3-e177-ffa5-ba6c639fb3dc.htm

This copies an element list from a document too another document.

 

I surprisingly found that when I copy the elements the command runs without errors ok and returns a list of ids.

 

Running the snippet below you my code  prints the "Element is null" in my console multiple times.

 

It turns out that some of the elements of those IDs are null. Looking at the IDs sequence, I found that the elements from the source model which have the same sequence with the element IDs of the destination model which are missing, are doors elements.

 

It seems that elements were created (that is why the "CopyElements" methods returns the Element Ids), however, those probably gets deleted. Would that be a case that all hosted elements are null from this list? If yes, how can I get their ID of the hosted copied elements?

 

 

using (Transaction tx = new Transaction(doc_Source, ("copying elements ")))
{
  tx.Start();
  CopyPasteOptions copy_paste_options = new CopyPasteOptions();
  translation = new XYZ();
  transform = Transform.Identity;
  ElementIDs_Destination=Autodesk.Revit.DB.ElementTransformUtils.CopyElements(doc_Source,ElementIDs_Source, doc_Destination, transform, copy_paste_options
);
  tx.Commit();
  foreach(ElementId i in CopiedElementsIDs)
  {
  if (doc_Destination.GetElement(i)== null)
  {
    Debug.Print("Element is null")
  }
}

 

 

Let me know if anyone wants more clarification or information to provide for this issue.

 

Any answer or similar experience will be really appreciated.

 

Many thanks,

Alex

0 Likes
1,549 Views
8 Replies
Replies (8)
Message 2 of 9

jeremytammik
Autodesk
Autodesk

I can share a different example of element ids getting nulled out, in case that provides any ideas: afaik, if an element A points to another element B by storing its element id in extensible storage, and A is copied whereas B is not, the element id in the extensible storage is nulled out.

  



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 9

jeremytammik
Autodesk
Autodesk

I see no connection in your code between `ElementIDs_Destination` and `CopiedElementsIDs`...

  



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 4 of 9

Anonymous
Not applicable

@jeremytammikmany thanks for your reply.

 

I am not sure what the extensive storage is and if this is relevant to my case when using the ElementTransformUtils.CopyElements method.

 

When I meant null elements I referred to the elements not the element IDs. To clarify my initial post, the list of elements IDs the ElementTransformUtils.CopyElements method returns have not null elements. This list is fine. However, when tying to retrieve the element by its element ID, I am getting a null element. Simply, CopyElements method return IDs which do not refer to existing elements in the document.

0 Likes
Message 5 of 9

Anonymous
Not applicable

Apologies for the confusion between `ElementIDs_Destination` and `CopiedElementsIDs`.

I wanted to make things more clear but I forgot to replace `CopiedElementsIDs` to `ElementIDs_Destination`.

See below the correct code.

 

When running this between two projects providing one wall and one door to the ElementIDs_Source variable, both elements are copied and both IDs are returned. However when the ID of the door (hosted element) is used to retrieve the door element, using the GetElement Method you get a null result. Weirdly, when checking the copied door, it has a different ID from the one returned from the CopyElements method. 

 

 

using (Transaction tx = new Transaction(doc_Source, ("copying elements ")))
{
  tx.Start();
  CopyPasteOptions copy_paste_options = new CopyPasteOptions();
  translation = new XYZ();
  transform = Transform.Identity;
  ElementIDs_Destination=Autodesk.Revit.DB.ElementTransformUtils.CopyElements(doc_Source,ElementIDs_Source, doc_Destination, transform, copy_paste_options
);
  tx.Commit();
  foreach(ElementId i in ElementIDs_Destination)
  {
  if (doc_Destination.GetElement(i)== null)
  {
    Debug.Print("Element is null")
  }
}

 

 

0 Likes
Message 6 of 9

RPTHOMAS108
Mentor
Mentor

There is not much information on how transactions should be used for this utility but I always assumed the destination document was the one you need an open transaction for as that is the document where new elements are being created. The source document is just providing items that don't change.

 

Whenever I've used it this has been the approach used, so try opening and committing a transaction on the destination document before iterating the list of ids. I believe this utility is leveraging copy/paste so I'm not sure if transactions are required, seems odd you've added to a document without opening a transaction for such.

Message 7 of 9

Anonymous
Not applicable

@RPTHOMAS108Apologies, I meant to write doc_Destination instead of doc_Source. I completely agree with your notes. You very correctly mentioned that CopyElements method needs a transaction for the destination and not the source document. 

 

using (Transaction tx = new Transaction(doc_Destination, ("copying elements ")))
{
  tx.Start();
  CopyPasteOptions copy_paste_options = new CopyPasteOptions();
  translation = new XYZ();
  transform = Transform.Identity;
  ElementIDs_Destination=Autodesk.Revit.DB.ElementTransformUtils.CopyElements(doc_Source,ElementIDs_Source, doc_Destination, transform, copy_paste_options
);
  tx.Commit();
  foreach(ElementId i in ElementIDs_Destination)
  {
  if (doc_Destination.GetElement(i)== null)
  {
    Debug.Print("Element is null")
  }
}

 

 

 

 

 

0 Likes
Message 8 of 9

jeremytammik
Autodesk
Autodesk

This CopyElements sample code opens a transaction on the target document, not the source, which definitely makes more sense, just as Richard suggested:

 

https://thebuildingcoder.typepad.com/blog/2020/06/set-crop-to-section-box-linked-boundaries-and-inte...

  



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 9 of 9

Anonymous
Not applicable

To give a better understnading about the issue I will try to provide a valid rvt model with some elements failing to be read using their id when those are copied over another file. I will post here again soon.

0 Likes