Announcements
Autodesk Community will be read-only between April 26 and April 27 as we complete essential maintenance. We will remove this banner once completed. Thanks for your understanding
Announcements
We are currently migrating data within this board to improve the community. During this process, posting, replying, editing and other features are temporarily disabled. We sincerely apologize for any inconvenience caused and appreciate your understanding. Thank you for your patience and support.

Exporting to DWG after Dynamo script being executed

joaofmoco
Advocate Advocate
946 Views
5 Replies
Message 1 of 6

Exporting to DWG after Dynamo script being executed

joaofmoco
Advocate
Advocate

Hello everyone,

I am developing an add-in for Revit that automatically opens a file, executes a Dynamo script and exports the views to a DWG file. However, I'm having issues implementing the last step. From what I understood, the code where I define the DWG export settings is not being executed. My only questions are: What's causing this? And what can I do to fix this?

The Command and App classes in C# are exposed, in order, bellow:

#region Namespaces
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Dynamo.Applications;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

#endregion

namespace DynamoAddin
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class Command : IExternalCommand
    {
        string Journal_Dynamo_Path = @"C:\Users\JoãoMoço\Documents\Dynamo\structure.dyn";

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //Get application and document objects and start transaction
            UIApplication uiapp = commandData.Application;
            Document doc = uiapp.ActiveUIDocument.Document;
            Application app = uiapp.Application;
            UIDocument uidoc = new UIDocument(doc);

            DynamoRevit dynamoRevit = new DynamoRevit();
            DynamoRevitCommandData dynamoRevitCommandData = new DynamoRevitCommandData();
            dynamoRevitCommandData.Application = commandData.Application;
            IDictionary<string, string> journalData = new Dictionary<string, string>
            {
                { Dynamo.Applications.JournalKeys.ShowUiKey, false.ToString() }, // don't show DynamoUI at runtime
                { Dynamo.Applications.JournalKeys.AutomationModeKey, true.ToString() }, //run journal automatically
                { Dynamo.Applications.JournalKeys.DynPathKey, Journal_Dynamo_Path }, //run node at this file path
                { Dynamo.Applications.JournalKeys.DynPathExecuteKey, true.ToString() }, // The journal file can specify if the Dynamo workspace opened from DynPathKey will be executed or not. If we are in automation mode the workspace will be executed regardless of this key.
                { Dynamo.Applications.JournalKeys.ForceManualRunKey, false.ToString() }, // don't run in manual mode
                { Dynamo.Applications.JournalKeys.ModelShutDownKey, true.ToString() },
                { Dynamo.Applications.JournalKeys.ModelNodesInfo, false.ToString() }

            };

            dynamoRevitCommandData.JournalData = journalData;
            Result externalCommandResult = dynamoRevit.ExecuteCommand(dynamoRevitCommandData);
            //return externalCommandResult;

            try
            {
                using (Transaction tx = new Transaction(doc))
                {
                    string dwgPath = "C:\\Users\\JoãoMoço\\Documents\\Dynamo";
                    tx.Start("ExportDWG");

                    doc.Regenerate();

                    ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();
                    foreach (ElementId e in selectedIds)
                    {
                        // Setting DWG Export Options
                        DWGExportOptions options = new DWGExportOptions();
                        ExportDWGSettings dwgSettings = ExportDWGSettings.Create(doc, "IBAS Export");
                        options = dwgSettings.GetDWGExportOptions();

                        options.Colors = ExportColorMode.TrueColorPerView;
                        options.FileVersion = ACADVersion.R2018;
                        options.MergedViews = true;
                        Element f = doc.GetElement(e);
                        ElementType ftype = doc.GetElement(f.GetTypeId()) as ElementType;
                        List<ElementId> collection = new List<ElementId>();
                        collection.Add(e);

                        // Export file as DWG
                        doc.Export(dwgPath, ftype.FamilyName + " " + f.Name, collection, options);
                        ElementId dwgSettingId = dwgSettings.Id;

                        doc.Delete(dwgSettingId);
                    }


                    tx.Commit();
                }

                return Result.Succeeded;
            }
            catch
            {
                return Result.Failed;
            }
        }
    }
}

 

#region Namespaces
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI;
using Dynamo.Applications;
using System;
using System.Collections.Generic;

#endregion

namespace DynamoAddin
{
    class App : IExternalApplication
    {
        string revitFilePath = @"C:\Users\JoãoMoço\Documents\Dynamo\Template.rvt";
        string commandId = "0a2ba4ad-c3a9-450d-aed1-4cfe6413228e";

        public Result OnStartup(UIControlledApplication a)
        {
            a.ControlledApplication.ApplicationInitialized += OnApplicationInitialized;

            return Result.Succeeded;
        }

        void OnApplicationInitialized(object sender, ApplicationInitializedEventArgs e)
        {
            Application app = sender as Application;
            UIApplication uiapp = new UIApplication(app);

            uiapp.OpenAndActivateDocument(revitFilePath);

            RevitCommandId idAddinCommand = RevitCommandId.LookupCommandId(commandId);
            uiapp.PostCommand(idAddinCommand);
        }

        public Result OnShutdown(UIControlledApplication a)
        {
            a.ControlledApplication.ApplicationInitialized -= OnApplicationInitialized;

            return Result.Succeeded;
        }
    }
}

 

The .sln file and the .dyn script are attached to this post.
Any feedback is helpful

Reply
Reply
0 Likes
Accepted solutions (1)
947 Views
5 Replies
Replies (5)
Message 2 of 6

joaofmoco
Advocate
Advocate

Update:

 

I just found out that the

ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();

 is not getting any Ids.
If anyone sees what is causing this, please let me know

Reply
Reply
0 Likes
Message 3 of 6

jeremy_tammik
Autodesk
Autodesk

Does the commandId constant refer to your Dynamo external command? I assume it does, so the PostCommand call actually launches your external command that in turn launches your Dynamo code via dynamoRevit.ExecuteCommand.

  

I can imagine the call to dynamoRevit.ExecuteCommand puts Revit into a state from which it will not return to execute the rest of the code following that statement.

  

I can imagine various ways to resolve this. 

  

My preference would be to rewrite the external Dynamo code and place the appropriate pure native Revit API code replacing it into the external command instead of calling dynamoRevit.ExecuteCommand. That would simplify the whole structure significantly.

  

If that is difficult or impossible, you might want to place the DWG export code into its own external command that is launched subsequently, after the Dynamo stuff. One approach to achieve that is being discussed right now as we speak in this parallel thread on how to chain Idling events:

  
https://forums.autodesk.com/t5/revit-api-forum/can-you-chain-idling-events/m-p/11484370

  

 

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

joaofmoco
Advocate
Advocate

Hello Jeremy and thank you for replying,

Yes, the commandId constant refers to your Dynamo external command.
The objective here was to chain a series of events together.
Originally, this was supposed to be all contained in C#, but a Dynamo script was developed in parallel since Dynamo as a built-in Excel import feature. 
When you speak of placing the DWG export code inside another External Command, do you mean inside of the same .sln file (Being another IExternalCommand class)?

Reply
Reply
Message 5 of 6

jeremy_tammik
Autodesk
Autodesk

> do you mean inside of the same .sln file?

  

Yes. Same VS SLN, and also same .NET assembly DLL.

  

> Being another IExternalCommand class?

  

Yes.

  

 
 
Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Reply
Reply
0 Likes
Message 6 of 6

joaofmoco
Advocate
Advocate
Accepted solution

Update on the situation:
I managed to circumvent the situation by adding the DWG Export functionality on the Dynamo script.

Thank you to everyone 🙂

Reply
Reply