Retrieve Data Exchanges
Once a Data Exchange is created, it gets hosted on a hosting provider, such as Autodesk Docs. Now if you want to retrieve this Data Exchange, you would need the Exchange Identifier/URN of the Data Exchange. The following section shows how you can get the Exchange Identifier of the exchange and other operations that can be performed on the exchange.
The Data Exchange Identifier
The following sections will guide you through identifying the exchange you want to load, retrieving the exchange identifier, and fetching the exchange data.
Identify Exchange to load
Exchanges are hosted on a hosting provider, such as Autodesk Docs. Each exchange is assigned a unique identifier by its hosting provider. On Autodesk Docs, this identifier is referred to as “entityId”. It can be extracted from the decoded URL of the exchange.
https://acc.autodesk.com/docs/files/projects/e3be8c87-1df5-470f-9214-1b6cc85452fa?folderUrn=urn%3Aadsk.wipprod%3Afs.folder%3Aco._BNKwsk-RZqkVuVAuuYjQA&entityId=`urn%3Aadsk.wipprod%3Adm.lineage%3A30m0VGWxR6CehOCs9TLcTQ`&viewModel=detail&moduleId=folders
Let’s break down the above URL to understand the other values that can be extracted:
- ProjectURN: e3be8c87-1df5-470f-9214-1b6cc85452fa
- folderURN: urn%3Aadsk.wipprod%3Afs.folder%3Aco._BNKwsk-RZqkVuVAuuYjQA&
- entityId: urn%3Aadsk.wipprod%3Adm.lineage%3A30m0VGWxR6CehOCs9TLcTQ
Retrieving the Data Exchange Identifier
The Data Exchange service assigns its own identifier to each Data Exchange, which is a combination of the CollectionId
, ExchangeId
, and HubId
. To retrieve this identifier, the SDK provides an API that accepts the inputs “exchangeUrn” and “collectionId.” In this case, the “exchangeUrn” is the entityId
(Note: Please use the decoded value) extracted from the URL mentioned above.
The CollectionId
can be retrieved using the GetCollectionIdAsync method available on the IClient interface, which accepts ProjectURN
(extracted from the URL above) as an input parameter and HubId
as an optional parameter. The API returns a unique identifier object specific to the Data Exchange.
var collectionId = await SDK.GetCollectionIdAsync("e3be8c87-1df5-470f-9214-1b6cc85452fa");
var ExchangeDetails = await SDK.GetExchangeDetailsAsync(collectionId,"urn:adsk.wipprod:dm.lineage:30m0VGWxR6CehOCs9TLcTQ").ConfigureAwait(false);
var ExchangeIdentifier = new DataExchangeIdentifier
{
CollectionId = ExchangeDetails.CollectionID,
ExchangeId = ExchangeDetails.ExchangeID,
HubId = ExchangeDetails.HubId
};
The ExchangeIdentifier
variable can now be used as an input to the GetExchangeDataAsync, GetExchangeRevisionsAsync, and SyncExchangeDataAsync methods.
Fetching an Exchange’s data
The GetExchangeDataAsync method returns the ExchangeData object, which is the client-side low-level data model for exchanges. The data returned represents the latest revision of the exchange at the time of retrieval.
ExchangeData ExchangeData = await SDK.GetExchangeDataAsync(ExchangeIdentifier);
The ElementDataModel.Create method provides a high-level abstraction of the Data Exchange data model.
ElementDataModel Model = ElementDataModel.Create(SDK, ExchangeData);
Retrieve the latest revision of a Data Exchange
You can call the GetExchangeRevisionsAsync method on the IClient interface, to retrieve a collection of revisions of the exchange. The revisions are sorted in chronological order starting from the most recent revision to the oldest. This method requires an instance of DataExchangeIdentifier as an input.
var ExchangeIdentifier = new DataExchangeIdentifier
{
CollectionId = ContainerID,
ExchangeId = ExchangeID,
HubId = HubId
};
//Get a list of all revisions
var Revisions = await Client.GetExchangeRevisionsAsync(ExchangeIdentifier);
var NewRevision = await Client.RetrieveLatestExchangeDataAsync(ElementDataModel);
var NewerRevisions = new List<string>();
if (!string.IsNullOrEmpty(NewRevision))
{
foreach (var Revision in Revisions)
{
if (Revision.Id == CurrentRevision)
{
break;
}
NewerRevisions.Add(revision.Id);
}
CurrentRevision = NewRevision;
}
Retrieve Parameters and ParameterSet of elements
Each Element of the Data Exchange contains the following collection properties:
- InstanceParameters
- TypeParameters (For Revit type parameters only)
Type parameters apply to every instance of a particular family type and are used to define properties that are common across all instances of that type. Changes to type parameters affect all instances of that type within the project. These parameters are useful for properties that should be consistent across multiple instances, such as material, dimensions, or other characteristics that do not vary from one instance to another.
For example, if you have a door family with a type parameter for door width, changing the width in the type properties will update the width for all doors of that type throughout the project. Instance parameters, on the other hand, apply to individual instances of a family type and allow for unique properties to be set for each instance. Changes to instance parameters affect only the specific instance of the element. These parameters are useful for properties that need to be unique for each instance, such as specific location, orientation, or a particular mark or identifier. For instance, if you have a door family with an instance parameter for door number, you can assign a unique door number to each door instance without affecting other doors of the same type.
All the above properties collectively encompasses all the parameters associated with the Element of the Data Exchange.
The InstanceParameters can contain both Parameters and ParameterSet. A ParameterSet contains a list of Parameters and these Parameters can hold both Parameter and ParameterSet
In the following code sample parameter.ParameterDataType
returns ParameterSet and ParameterSet.Parameters returns a list of parameters.
The Parameters obtained from the ParameterSet are same as simple parameter which contains a schemaid and its value and type.
private void ExtractParameterData(Parameter parameter)
{
if (parameter.ParameterDataType == ParameterDataType.ParameterSet)
{
// Extract parameter set data like ParameterSet id, name, description, and parameters
Console.WriteLine((parameter as ParameterSet).Id);
Console.WriteLine((parameter as ParameterSet).Name);
Console.WriteLine((parameter as ParameterSet).Description);
Console.WriteLine((parameter as ParameterSet).Parameters.Count);
foreach (var param in (parameter as ParameterSet).Parameters)
{
ExtractParameterData(param);
}
}
else
{
// Extract parameter data like Parameter name, type, value, etc.
Console.WriteLine(parameter.Name);
Console.WriteLine(parameter.ParameterDataType);
Console.WriteLine(parameter.Value);
}
}
foreach (var parameter in Element.InstanceParameters)
{
ExtractParameterData(parameter);
}
foreach (var parameter in Element.TypeParameters) // For Revit type parameters only.
{
// Implement any required operations for type parameters here
}
// or
var typeParametersDict = data.GetTypeParameters(new List<string> { "element type1", "element type2" });
foreach (var item in typeParametersDict)
{
foreach (var parameter in item.Value)
{
ShowParameter(parameter);
}
}
In the above code, data
represents instance of ElementDataModel which can be created using the ElementDataModel.Create method.
Retrieve Transformation of elements
The Element object contains a Transformation attribute using which you can retrieve the transformation of elements.
var Transformation = Element.Transformation
Retrieve element Geometry
Primitive, BRep/Mesh: In ElementDataModel, you will find a method named GetElementGeometriesByElementsAsync. This method returns a dictionary of Elements and their associated geometries.
Dictionary<Element, IEnumerable<ElementGeometry>> ElementGeometriesMap = ElementDataModel.GetElementGeometriesByElementsAsync(ElementDataModel.Elements)