1

intro: I'm trying to create a custom control for use within autocad through autocad .net API.

I've created the basic design of the UC, and decided to group some elements of it in a custom UC and call it later to the main UC. (basically for code management, organisation... leaving me with a code structure like this:

UC(top)-consisting of few custom child UCs each of those Child UCs consisting of few buttons, and combo boxes.

simple so far...

in those child UCs, I call on a simple ACAD method/object.

the problem appears when I try adding one of those Child UCs to the main UC. I get a "CLR error" (image appended).

https://s32.postimg.org/6gnt3sgz9/why_no_work.jpg

I've tried few methods of solving the problem; 1) Using the Custom Controls instead of UC 2) tried to clear solution, rebuild solution. 3) tried creating a separate Class that calls on ACAD methods from outside the UC code

basically, what I've learned is, that I can (somehow) get the program to work if I run the code from within user created events (such as button click,...), but if I try calling acad command from within UC constructor, or UC_Load event method, I end up getting the CLR error when ever I try adding the childUC (through VSdesigner, and if I add it by code, I end up raising the error whenver I try calling on the designer)

if nothing else, I'd like to know why the code behaves differently depending if the code is called from "UC_load" or "button_clicked".

here's my code:

public partial class child : UserControl
{

    public child()
    {
        InitializeComponent();
        //if I initialize doc here, I get the error
    }

    private void child_Load(object sender, EventArgs e)
    {
        //if I initialize doc here, I get the error

    }

    private void button1_Click(object sender, EventArgs e)
    {
        //it works here, but Im unsure why
        Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
    }
}

"the Document doc =..... is the line that generates the error;

Here's an image of the error, and the solution. The error appears when I klik the child object to the designer, or before I open the designer after I've added the "child object" from the code.

using C# acad 2017, VS2015, x64

Veve
  • 6,643
  • 5
  • 39
  • 58

1 Answers1

2

Is this happening while you're creating the form, right?

This might happen as VS tried to load the object (in your case, the AutoCAD Document object), which is not possible, but might also happen as VS is 32 and AutoCAD references are 64. Consider use the AutoCAD references from the ObjectARX SDK, as these DLLs are just the method stubs.

I would suggest (as a best practice) to do not use AutoCAD objects inside the form, but rather separate UI and biz logic. Separation of UI and biz logic means, at least, have a class that is purely based on UI components (e.g. Windows.Form namespace), another class that implementes the actual AutoCAD command (i.e. AcMgd.dll & AcCoreMgd.dll objects) and, finally, the data layer (i.e. AcDbMgd.dll objects)

Augusto Goncalves
  • 8,493
  • 2
  • 17
  • 44
  • I never get to compiling part, -error happens while adding one UC to another. The point is to add the main UC to acads GUI , which accepts only UCs not form its interesting to note that EVERYTHING works while all elements are inside only one UC (the Acad api code and UC logics). But at the moment I decided to separate code into few "sub UCs" the error appears. Its like VS is expecting all custom UCs to have integrated logics inside them, and when adding them to another UC, he finds "loose ends", that he doesnt please. could you elaborate what you mean by "separate UI and biz logic"? – Lucano Deskovic Jul 15 '16 at 18:57
  • are you adding it to the PaletteSet? My suggestion to separate UI & biz logic still applies and will allow you to reuse your logic on the command line, for instance – Augusto Goncalves Jul 15 '16 at 18:59
  • 1
    exactly. I'm adding it to PS. I edited the previous comment. Ive tried separating the acad part to a separate class, but when I call methods from that class/objects, I still get an error, when calling it in UC constructor or UC_Load. I'm not sure what you mean by "separating UI & Biz". Do you mean just separate classes, or precompile biz part to dll, or use sendstring to execute, or some 3rd method that I didnt think off? – Lucano Deskovic Jul 15 '16 at 19:04
  • I edit the reply. VS may be failing as it tries to instantiate the Document object, which is not possible. – Augusto Goncalves Jul 15 '16 at 19:09
  • I've tried creating the program as clean as possible (separating the classes, etc), and have couple of concepts I want to try out,-passing document doc, through an arguement UC(document doc) – Lucano Deskovic Jul 16 '16 at 11:37
  • I've tried creating the program as clean as possible (separating the classes, still some issues happen etc), and have couple of concepts I want to try out, before I continue the debate. However, I'd really like to know why the behaviour of the program is different when it encounters "document doc..." line in constructor, "form_load", and "button_clicked"... simply to learn more about the background mechanics of the UCs. ATTM, I'll experiment with tips from this thread: http://stackoverflow.com/questions/1166226/detecting-design-mode-from-a-controls-constructor – Lucano Deskovic Jul 16 '16 at 11:43