Search code examples
autodesk-forgeautodesk-designautomation

Does my custom CAD script created with .NET require a command for quitting?


I am trying to execute a CAD script on a DWG file stored in my bucket using the Design Automation API. It just writes "Hello World!!!" on the file.

In order to create the script, I followed along with this tutorial:

https://help.autodesk.com/view/OARX/2019/ENU/?guid=GUID-BA686431-C8BF-49F2-946E-9CEB2F7AE4FA

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;

namespace MyFirstProject
{
    public class Class1
    {
        [CommandMethod("AdskGreeting")]
        public void AdskGreeting()
        {
            // Get the current document and database, and start a transaction
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;

            // Starts a new transaction with the Transaction Manager
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // Open the Block table record for read
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                             OpenMode.ForRead) as BlockTable;

                // Open the Block table record Model space for write
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                OpenMode.ForWrite) as BlockTableRecord;

                /* Creates a new MText object and assigns it a location,
                text value and text style */
                using (MText objText = new MText())
                {
                    // Specify the insertion point of the MText object
                    objText.Location = new Autodesk.AutoCAD.Geometry.Point3d(2, 2, 0);

                    // Set the text string for the MText object
                    objText.Contents = "Hello World!!!";

                    // Set the text style for the MText object
                    objText.TextStyleId = acCurDb.Textstyle;

                    // Appends the new MText object to model space
                    acBlkTblRec.AppendEntity(objText);

                    // Appends to new MText object to the active transaction
                    acTrans.AddNewlyCreatedDBObject(objText, true);
                }

                // Saves the changes to the database and closes the transaction
                acTrans.Commit();
            }
        }
    }
}

I made my way through the Design Automation workflow. I was able to post an AppPackage, post an Activity, and post a WorkItem using the Forge Node.js SDK.

However, the status of the WorkItem came back as FailedExecution.

I won't show the entire error log because it contains confidential information, but here are some highlights:


[01/17/2019 21:30:44] End download phase.
[01/17/2019 21:30:44] Start preparing script and command line parameters.
[01/17/2019 21:30:44] Start script content.
[01/17/2019 21:30:44] _ADSKGREETING
[01/17/2019 21:30:44] End script content.

//Blah

[01/17/2019 21:30:44] End preparing script and command line parameters.
[01/17/2019 21:30:44] Start script phase.

//Blah

[01/17/2019 21:30:44] Start AutoCAD Core Engine standard output dump.

//Blah blah blah

[01/17/2019 21:30:44] AutoCAD Core Engine Console - Copyright 2015 Autodesk, Inc.  All rights reserved. (M.49.Z.1)
[01/17/2019 21:30:44] Running at low integrity.
[01/17/2019 21:30:45] Loading AEC Base...
[01/17/2019 21:30:45] Loading AEC Base Extended...
[01/17/2019 21:30:45] Loading AEC Project Base...
[01/17/2019 21:30:45] Loading AEC Architectural Base...
[01/17/2019 21:30:46] Loading AEC Schedule...
[01/17/2019 21:30:46] Substituting [simplex.shx] for [fed-s.shx].
[01/17/2019 21:30:46] Substituting [simplex.shx] for [fed-s.shx].
[01/17/2019 21:30:46] Substituting [simplex.shx] for [fed-s.shx].
[01/17/2019 21:30:46] Substituting [simplex.shx] for [fed-l.shx].
[01/17/2019 21:30:46] Substituting [simplex.shx] for [fed-s.shx].
[01/17/2019 21:30:46] Substituting [simplex.shx] for [fed-s.shx].
[01/17/2019 21:30:46] Substituting [simplex.shx] for [fed-l.shx].
[01/17/2019 21:30:46] Substituting [simplex.shx] for [fed-s.shx].
[01/17/2019 21:30:46] Substituting [simplex.shx] for [fed-l.shx].
[01/17/2019 21:30:46] Substituting [simplex.shx] for [fed-l.shx].
[01/17/2019 21:30:46] Substituting [simplex.shx] for [fed-s.shx].
[01/17/2019 21:30:46] Regenerating model.
[01/17/2019 21:30:47] Command:
[01/17/2019 21:30:47] Command:
[01/17/2019 21:30:47] Command:
[01/17/2019 21:30:47] Command: _ADSKGREETING_quit
[01/17/2019 21:30:47] Unknown command "ADSKGREETING_QUIT".  Press F1 for help.
[01/17/2019 21:31:47] Error: AutoCAD Core Console is shut down due to timeout.
[01/17/2019 21:31:47] End script phase.
[01/17/2019 21:31:47] Error: An unexpected error happened during phase CoreEngineExecution of job.
[01/17/2019 21:31:47] Job finished with result FailedExecution

I assume the script is fine because I can run it successfully with AutoCAD on my computer by doing this:

NETLOAD -> select the MyFirstProject.dll file -> ADSKGREETING

Is something missing from my script? Do I have to include a command for quitting the script? If so, how?


Solution

  • Yes and no. If there’s no task to perform in the termination stage simply leave the handler empty. But you will need to specify the necessary assembly atttibutes at the start of your script nonetheless. See here for details.

    [assembly: CommandClass(typeof(MyFirstProject.Class1))]
    [assembly: ExtensionApplication(null)]
    
    namespace ...