Fatal Error Trying to Run Add-In

Fatal Error Trying to Run Add-In

TCARPENTER21
Enthusiast Enthusiast
1,137 Views
5 Replies
Message 1 of 6

Fatal Error Trying to Run Add-In

TCARPENTER21
Enthusiast
Enthusiast

So I have an Add-in, which I call from a COM application, but have added CommandMethods to for testing.  When I try to run the CommandMethod within AutoCAD - I get an immediate fatal error.  I'm not able to capture any kind of error other than this:

 

 

I'm running AutoCAD2016, and Visusal Studio2013.

Here is my Add-In code - any suggestions as to what I'm doing wrong?

 

namespace HM.ACAD_Add_In
{
    [Guid("704E842F-0CA5-4360-A932-C45186AE23AE"), ComVisible(true)]
    public interface IAttribAdd
    {
        [DispId(1)]
        string AddAttribs(string fileToOpen, string DWGSaveName, string[] layerInfo, string[] attDefInfo);
        string ModAttribs(string fileToOpen, string DWGSaveName, string[] layerInfo, string[] attDefInfo);
    }

    [ProgId("Attribute.Commands"),
     Guid("D1B6C2D5-E815-4E2A-B4B7-BDA7C0BA81FD"),
     ClassInterface(ClassInterfaceType.None)]

    public class Commands : ServicedComponent, IAttribAdd
    {

        [CommandMethod("INFORTEST")]
        public void InforTest() // This method can have any name
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            
            if (doc != null)
            {
                Editor ed = doc.Editor;
                ed.WriteMessage("\nInfor.AttribAdd is loaded.");
            }
        }

        [CommandMethod("ACISIN_TEST")]
        public void acisin_test() // This method can have any name
        {
            OpenACIS(@"C:\BookCase24x16x68.SAT");
        }

        [CommandMethod("ADDLAYERS_TEST")]
        public void addlayers_test()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database acDb = doc.Database;
            Editor ed = doc.Editor;
            
            string[] stringLayers = {"STORAGE", "MAGENTA", 
                                     "STORAGE-CAPPN", "WHITE",
                                     "STORAGE-CAPPD", "WHITE",
                                     "STORAGE-CAPMG", "WHITE",
                                     "STORAGE-CAPMC", "WHITE"};
            //AddLayers(stringLayers, doc, acDb);
        }
        
        [CommandMethod("ADDATTRBS_TEST")]
        public void addattrbs_test()
        {
            
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database acDb = doc.Database;
            Editor ed = doc.Editor;
            string message = "None";

            string[] stringAttrbs =  {"CAPPN", "PartNumber", "", "True", "False", "False", "False", "True", "False", "5,-5,0", "2", "BottomLeft", "Arial", "STORAGE-CAPPN", "WHITE", 
                                      "CAPPD", "PartDescription", "", "True", "False", "False", "False", "True", "False", "5,-7.5,0", "2", "BottomLeft", "Arial", "STORAGE-CAPPD", "WHITE", 
                                      "CAPMG", "ManufacturingCode", "", "True", "False", "False", "False", "True", "False", "5,-10,0", "2", "BottomLeft", "Arial", "STORAGE-CAPMG", "WHITE", 
                                      "CAPMC", "CatalogCode", "", "True", "False", "False", "False", "True", "False", "5,-12.5,0", "2", "BottomLeft", "Arial", "STORAGE-CAPMC", "WHITE"};

            message = AddAttributeDefs(stringAttrbs);

            ed.WriteMessage("\n" + message);

        }

        [CommandMethod("V_NET")]
        public void V_Net()
        {
            VPointNetClass VP = new VPointNetClass();
            VP.VPointNet(225, 35);
        }
        
        [CommandMethod("ZOOM_TEST")]
        public void zoom_test()
        {
            //Zoom(new Point3d(), new Point3d(), new Point3d(), 1.01075);
            Zoom(new Point3d(265.1, -404.7, 1579.0), new Point3d(838.0, 168.2, 1006.2), new Point3d(0.4, 0.4, 0.8), 1);
        }
        
        [CommandMethod("ModAttribs_Test")]
        public void modattribs_test()
        {
            TextWriterTraceListener tr1 = new TextWriterTraceListener(System.IO.File.CreateText("C:\\Output.txt"));
            Debug.Listeners.Add(tr1);

            tr1.WriteLine("Inside ModAttribs_Test");
            Document doc = Application.DocumentManager.MdiActiveDocument;

            tr1.WriteLine("Got active document.");
            Database acDb = doc.Database;

            tr1.WriteLine("Got current database inside active document.");
            Editor ed = doc.Editor;

            tr1.WriteLine("Set editor to current document editor.");
            string message = "None";

            string dwgToProcess = "C:\\TDCI2D\\HermanMiller\\PhaseII\\554091T.DWG";
            string dwgToSave = "C:\\TDCI2D\\HermanMiller\\PhaseII\\554091T_.DWG";
            
            string[] lyrs = {"Test", "WHITE"};
            
            string[] stringAttrbs =  {"CAPPN", "PartNumber", "", "True", "False", "False", "False", "True", "False", "5,-5,0", "2", "BottomLeft", "Arial", "STORAGE-CAPPN", "WHITE", 
                                      "CAPPD", "PartDescription", "", "True", "False", "False", "False", "True", "False", "5,-7.5,0", "2", "BottomLeft", "Arial", "STORAGE-CAPPD", "WHITE", 
                                      "CAPMG", "ManufacturingCode", "", "True", "False", "False", "False", "True", "False", "5,-10,0", "2", "BottomLeft", "Arial", "STORAGE-CAPMG", "WHITE", 
                                      "CAPMC", "CatalogCode", "", "True", "False", "False", "False", "True", "False", "5,-12.5,0", "2", "BottomLeft", "Arial", "STORAGE-CAPMC", "WHITE"};

            
            tr1.WriteLine("set dwgToProcess, dwgToSave, lyrs, and stringAttribs");
            tr1.WriteLine("Calling Mod_Attribs.");

            message = ModAttribs(dwgToProcess, dwgToSave, lyrs, stringAttrbs);

            ed.WriteMessage("\n" + message);
            tr1.Flush();

        }
0 Likes
1,138 Views
5 Replies
Replies (5)
Message 2 of 6

ActivistInvestor
Mentor
Mentor
Does your code compile? I don't see any implementation of the COM interface.

It's probably not a good idea to derive a class exposing commands from ServicedComponent.

I would put the command methods in another class (and declare them to be static if a separate instance of the class for each open document is not necessary).
0 Likes
Message 3 of 6

TCARPENTER21
Enthusiast
Enthusiast

Hmmm.... I was following Kean Walmsley's example code, but maybe that's not the best way, it does compile and load, it just tanks immediately when I run ModAttrbs_Test.

 

So I did find my issue, but it was much further down in the code than I thought, and it turned out I had a null pointer to one of my objects.  Once I fixed that, it's behaving better, but it seems every error is causing AutoCAD to crash, so maybe the serviced component is the wrong place - just not sure what else I would do, I'm still a newbie at C#, but I do need the return values from the add-ins as part of another process...

0 Likes
Message 4 of 6

ActivistInvestor
Mentor
Mentor
I haven't seen the example code you based your code on, but mixing non-static command methods in a COM class is probably not a good idea, and doesn't serve a useful purpose.

I would suggest defining a separate class for exposing COM methods if your command methods are not static.
0 Likes
Message 5 of 6

TCARPENTER21
Enthusiast
Enthusiast

Okay, looks like I'll have to do a bit of homework, thanks for the input.

0 Likes
Message 6 of 6

norman.yuan
Mentor
Mentor

Based on what you have said here, it looked like your original post was somehow "misleading": AutoCAD is not crashing immediately, it is because your faulty code after all, and there is error capture mechanism in your code at all to support your claim of "not able to capture any kind of...". Maybe, you mistakenly thought "TraceListener" as error capturing? If so, it is not. It is just a regular code in the execution flow, any unhandled exception would stop the code execution. 

 

Yes, most unhandled exception in AutoCAD .NET API, when the exception is raised by AutoCAD .NET API classes, will crash AutoCAD, because the code in executed insdie the AutoCAD process. If you have sound exception handling (mostly try{...} catach {...} block), you would have already found the reason of crashing, or if you step through the code in debugging, you can easily find the said NullOblject exception. So, debug your code, step through line by line, then you would be able to post more accurate questions (or better yet, you may be able to answer most questions before you even ask).

 

I assume you are trying to use AutoCAD as a COM server so an external app can communicate/automate AutoCAD to do something with drawings. While I tend to consider automating AutoCAD from external app is not a good solution in most cases, one should consider use AutoCAD's built-in COM API first. Exposing .NET API functionality as COM should only be considered as LAST option unless you know you need AutoCAD do to something that can only be done with its .NET API. Even the unique thing you want AutoCAD to do can only be materialized with its .NET API, you may still can bypass the COM exposure sometimes. Well, I am trying to say is, exposing .NET API functionality to COM in most cases are not worth the trouble.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes