Hi,
Unfortunately there is no API that can copy elements or types directly from one document to another at this time.
We can only duplicate new types within the same document. We cannot insert types from one document to another document.
As a workaround, we can create corresponding elements in another document, then copy the parameters or properties one by one via API. For instance, when copy a wall type, we need to use WallType.Duplicate() to create a new wall type with the same name, then set each parameter's value the same as the original wall type.
For example, there are two documents,
docActive: The document that is active
docHide: The document that is created by opening a rvt file or NewProjectDocument() method. It is invisible and cannot be switched to active.
We will to copy one wall type in docHide to docActive. Here is the steps to copy value.
Get one of wall type in docActive, assign to wt1,
Duplicate this type using the target name by wt1.Duplicate(TargetName), and assign to wtExternal,
Retrieve the properties and parameter values from target wall type in docHide, and assign the values to wtExternal in docActive. We can use the reflector mechanism to retrieve and assign property values without knowing the property name. Also we can iterate all parameters in a loop without knowing each parameter name.
Here is an example of the code that is used for same question:
Document doc = _commandData.Application.OpenDocumentFile(fileName);
Document docActive = _commandData.Application.ActiveDocument;
doc.BeginTransaction();
Autodesk.Revit.Symbols.WallType dupWT = null;
foreach (Autodesk.Revit.Symbols.WallType wt in docActive.WallTypes)
{
dupWT = (Autodesk.Revit.Symbols.WallType)wt.Duplicate(wt.Name + "Dup");
break; //break here, for we only copy one type.
}
//get the target wall type in doc.
Autodesk.Revit.Symbols.WallType wtTarget = null;
foreach (Autodesk.Revit.Symbols.WallType wt in docActive.WallTypes)
{
wtTarget = wt
break; //break here, for we find the target.
}
//copy properties from wtTarget to dupWT.
//We can also assign values via .NET reflector. So we can do this for FloorType, or other any types via the same code.
//For simplicity, I assign two properties here line by line. This requires we know the property name. This is not flexible.
dupWT.Width = wtTarget.Width;
dupWT.Kind = wtTarget.Kind;
...
//copy parameter values from wtTarget to dupWT.
Parameter para= null;
For Each( para in wtTarget.Parameters)
{
Switch(para.StorageType)
Case StorageType.Double:
dupWT.get_Parameter(para.Name).Set(wt.AsDouble);
Case storageType.ElementId:
dupWT.get_Parameter(para.Name).Set(wt.AsElementId);
//and so on for other storage types.
...
}
doc.EndTransaction();
I hope this helps.
Akira Kudo
Developer Technical Services
Autodesk Developer Network