How to post new version file BIM 360 cloud model using Design Automation?

74 Views Asked by At

I have a problem when I'm try to update new parameter override to BIM 360 cloud Revit model and post a new publish version for Revit model,

Example: The latest version model in BIM 360 with file name sample.rvt is version 1 . After set parameter will be become the file name sample.rvt with version 2.

These are the steps I'm tried to achieve :

  • Create bundle app update parameters
    private void DoJob(DesignAutomationData data)
    {
        if(data.RevitDoc == null) throw new Exception("Have problem with DesignAutomationData.RevitDoc");
        Document doc = data.RevitDoc;
        List<InputParams> inputParams = GetParamsJson();
        string result = SetParameterCommand.Execute(doc, inputParams);
        File.WriteAllText("result.txt", result);
    }
    
  • Create activity
    Activity activitySpec = new Activity()
    {
       // Activities 
       ... 
    }
    
  • Execute work item
    private async Task<Status> ExecuteWorkItem(string forgeToken,List<InputParams> inputParams, string projectId, string versionId, string callBackUrl)
        ...
    
  • Post new version - I don't know how to implement this

I failed with the last step, post new version, because I can't find any sample on GitHub for post new version from BIM360 Model just saved as file to bucket. If you have any C# code sample, I'd appreciate it if you shared it.

2

There are 2 best solutions below

0
ChuongHo On BEST ANSWER

Finally I also have answer for my problem at this document: https://aps.autodesk.com/en/docs/bim360/v1/tutorials/document-management/upload-document-s3/

This is step by step I resolved my problem :

  1. Create a new storage object

  2. Check file item exist and use this item if it exit.

  var items = await GetFolderItems(projectId, folderUrn, accessToken).ConfigureAwait(false);
        var item = items.Cast<KeyValuePair<string, dynamic>>().FirstOrDefault(item =>
            item.Value.attributes.displayName.Equals(filename, StringComparison.OrdinalIgnoreCase));
  1. Post a update version :
  • In case item not exitss, we create a new one .
  • In case item exites, keep use item id at previous version like urn:adsk.wipprod:dm.lineage:J-CiZHzFTGy-A0-RfhEUMQ:
 private static async Task<string> UpdateVersionAsync(string projectId, string itemId, string objectId,
        string filename, string accessToken)
    {
        var versionsApi = new VersionsApi();
        versionsApi.Configuration.AccessToken = accessToken;

        var relationships = new CreateVersionDataRelationships
        (
            new CreateVersionDataRelationshipsItem
            (
                new CreateVersionDataRelationshipsItemData
                (
                    CreateVersionDataRelationshipsItemData.TypeEnum.Items,
                    itemId
                )
            ),
            new CreateItemRelationshipsStorage
            (
                new CreateItemRelationshipsStorageData
                (
                    CreateItemRelationshipsStorageData.TypeEnum.Objects,
                    objectId
                )
            )
        );
        var createVersion = new CreateVersion
        (
            new JsonApiVersionJsonapi
            (
                JsonApiVersionJsonapi.VersionEnum._0
            ),
            new CreateVersionData
            (
                CreateVersionData.TypeEnum.Versions,
                new CreateStorageDataAttributes
                (
                    filename,
                    new BaseAttributesExtensionObject
                    (
                        "versions:autodesk.bim360:File",
                        "1.0",
                        new JsonApiLink(string.Empty),
                        null
                    )
                ),
                relationships
            )
        );

        dynamic versionResponse = await versionsApi.PostVersionAsync(projectId, createVersion).ConfigureAwait(false);
        var versionId = versionResponse.data.id;
        return versionId;
    }
2
Chathura Abeywickrama On

Check this code.

private void PostNewVersion(string forgeToken, string projectId, string itemId, string newVersion, string callbackUrl)
{
    RestClient client = new RestClient("https://developer.api.autodesk.com/data/v1/projects/" + projectId + "/versions");
    RestRequest request = new RestRequest(Method.POST);
    request.AddHeader("Authorization", "Bearer " + forgeToken);
    request.AddHeader("Content-Type", "application/json");
    
    // Prepare the request body with version details
    string requestBody = $"{{\"data\": {{\"type\": \"versions\",\"attributes\": {{\"name\": \"YourModelName\",\"extension\": {{\"type\": \"versions:autodesk.bim360:File\",\"version\": \"{newVersion}\"}}}},\"relationships\": {{\"item\": {{\"data\": {{\"type\": \"items\",\"id\": \"{itemId}\"}}}}}}}},\"jsonapi\": {{\"version\": \"1.0\"}}}}";
    request.AddParameter("application/json", requestBody, ParameterType.RequestBody);

    // Execute the request
    IRestResponse response = client.Execute(request);
    
    // Handle the response
    if (response.StatusCode == HttpStatusCode.Created)
    {
        // Version created successfully
        string versionId = response.Headers
            .FirstOrDefault(h => h.Name == "Location" && h.Value.ToString().Contains("versions"))
            ?.Value?.ToString();
        
        // Now, you can associate the storage location with the new version (not shown in this example)
        UpdateStorageLocation(forgeToken, projectId, versionId, callbackUrl);
    }
    else
    {
        // Handle error
        Console.WriteLine("Error creating version: " + response.Content);
    }
}

private void UpdateStorageLocation(string forgeToken, string projectId, string versionId, string callbackUrl)
{
    // Implement the logic to update storage location
    // Use the projects/:project_id/storage endpoint to associate the model with the new version
    // Refer to Forge Data Management API documentation for details
}