Message 1 of 6
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
Solved! Go to Solution.
Reply
Reply