Create .NET view control at runtime on 64bits

By Xiaodong Liang

Typically, to work with the application of view control of .NET, we need to add the control to our application  (say an Windows Form) firstly. In the designer of Visual Studio, we will add the reference Autodesk.Navisworks.Controls.dll. It will generate two controls ( View control and Document control )  in the designer:

image

 

image

After adding the instances of the document control and view control, we can start to use them in the application. The help reference [Developer Guide] >> [Using the Controls] tells in details.

Unfortunately, this workflow is only applicable on 32bits. On 64bits,  if you try to add the reference Autodesk.Navisworks.Controls.dll in the [Designer ] >> [Choose Toolbox Items], you will receive an error:

"Autodesk.Navisworks.Controls.dll is not a valid .NET module".

Actually, this is not an issue of Navisworks API, but a known issue of Visual Studio. In short,  the behavior is by design. Visual Studio is a 32-bit process, and therefore can only execute 32-bit modules. While Visual Studio allows you to add a reference to a 64-bit assembly, it cannot actually JIT compile it to 64-bit and execute it in process. This article of MSDN knowledge base tell more:

http://support.microsoft.com/kb/963017

The solutions are:

1) either create the application on 32bits with the option [Any CPU], and copy it to 64bits. You are able to run the application directly because it will load the 64bits control at runtime. Even you can see it in the designer, so you can still re-design the view control with its properties.

2) or, you can add the controls at runtime. If you dig into the normal workflow of a Windows Form application, you can see each form class has one partial class in the MyForm.Designer.cs, where all the controls are initialized and added to the form. So we can mimic the workflow and add the controls dynamically. Of course, you need to be careful to set location/width/height of the view control to make it nice in visual.

static class Program
 {
private Autodesk.Navisworks.Api.Controls.DocumentControl documentControl1;
    private Autodesk.Navisworks.Api.Controls.ViewControl viewControl1;
 
    public Form1()
    {
        // Initialize other controls
        InitializeComponent();
 
        //Initialize document control
        if(this.components == null)
            this.components = new System.ComponentModel.Container();
        this.documentControl1 = new Autodesk.Navisworks.Api.Controls.DocumentControl(this.components);
 
        // Initialize view control
        this.viewControl1 = new Autodesk.Navisworks.Api.Controls.ViewControl();
 
 
        // attach the document control to the view control
        this.viewControl1.DocumentControl = this.documentControl1;
 
        //set the location, width and height of the control
        this.viewControl1.Location = new System.Drawing.Point(Left + 10 , Top -10 );             
        this.viewControl1.Name = "viewControl1";
        this.viewControl1.Size = new System.Drawing.Size(Width - 20 , Height  -20);
        this.viewControl1.TabIndex = 0;
        this.viewControl1.Text = "viewControl1";
 
        // add it to the layout
        this.Controls.Add(viewControl1); 
    }
}

Finally, do not forget to add the lines of initializing application of control:

static void Main()
{
    //Set to single document mode
    Autodesk.Navisworks.Api.Controls.ApplicationControl.ApplicationType = ApplicationType.SingleDocument;
    //Initialise the api
    Autodesk.Navisworks.Api.Controls.ApplicationControl.Initialize();
 
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
 
    //Finish use of the API.
    Autodesk.Navisworks.Api.Controls.ApplicationControl.Terminate();
}

Comments

2 responses to “Create .NET view control at runtime on 64bits”

  1. Allockse Avatar
    Allockse

    I had a question regarding some debuging information that shows up randomly when I’m invoking ApplicationControl.Initialize()/Terminate().
    Initially when I created my Navisworks .NET project everything ran beautifully, however now although it still works, I am getting diagnostic information reported into my console application’s output. These entries appear when ApplicationControl.Initialize is invoked:
    DEBUG: Module 1, diagnostic code 234 [ “lidrvwin” 30 ]
    DEBUG: Module 1, diagnostic code 234 [ “limultit” 82 ]
    DEBUG: Module 1, diagnostic code 234 [ “lilwaio” 150 ]
    DEBUG: Module 1, diagnostic code 234 [ “livm” 154 ]
    DEBUG: Module 1, diagnostic code 234 [ “lishaddb” 155 ]
    DEBUG: Module 1, diagnostic code 234 [ “liplugin” 148 ]
    DEBUG: Module 1, diagnostic code 234 [ “liplgman” 164 ]
    DEBUG: Module 1, diagnostic code 234 [ “liview” 9 ]
    DEBUG: Module 1, diagnostic code 234 [ “ligeom” 2 ]
    DEBUG: Module 1, diagnostic code 234 [ “lixform” 15 ]
    DEBUG: Module 1, diagnostic code 234 [ “lipoly” 3 ]
    DEBUG: Module 1, diagnostic code 234 [ “lishader” 5 ]
    DEBUG: Module 1, diagnostic code 234 [ “liitface” 140 ]
    DEBUG: Module 1, diagnostic code 234 [ “liraycor” 136 ]
    DEBUG: Module 1, diagnostic code 234 [ “lirayshw” 138 ]
    DEBUG: Module 1, diagnostic code 234 [ “lirayful” 137 ]
    DEBUG: Module 1, diagnostic code 234 [ “lirenwir” 10 ]
    DEBUG: Module 1, diagnostic code 234 [ “lishslte” 73 ]
    DEBUG: Module 1, diagnostic code 234 [ “lishlite” 74 ]
    DEBUG: Module 1, diagnostic code 234 [ “lishclas” 75 ]
    DEBUG: Module 1, diagnostic code 234 [ “lishray” 139 ]
    DEBUG: Module 1, diagnostic code 234 [ “lishpro” 76 ]
    DEBUG: Module 1, diagnostic code 234 [ “lirenstd” 12 ]
    DEBUG: Module 1, diagnostic code 234 [ “lirenadv” 32 ]
    DEBUG: Module 1, diagnostic code 234 [ “ligeops” 22 ]
    DEBUG: Module 1, diagnostic code 234 [ “lirenhid” 25 ]
    DEBUG: Module 1, diagnostic code 234 [ “lidrvexr” 157 ]
    DEBUG: Module 1, diagnostic code 234 [ “lidrvlwi” 28 ]
    DEBUG: Module 1, diagnostic code 234 [ “lidrvbmp” 38 ]
    DEBUG: Module 1, diagnostic code 234 [ “lidrvtga” 39 ]
    DEBUG: Module 1, diagnostic code 234 [ “lidrvtif” 40 ]
    DEBUG: Module 1, diagnostic code 234 [ “lidrvps” 41 ]
    DEBUG: Module 1, diagnostic code 234 [ “lidrvdum” 37 ]
    DEBUG: Module 1, diagnostic code 234 [ “lidrvjpg” 79 ]
    DEBUG: Module 1, diagnostic code 234 [ “lidrvpng” 168 ]
    DEBUG: Module 1, diagnostic code 234 [ “lidrvhdr” 160 ]
    DEBUG: Module 1, diagnostic code 234 [ “liradios” 6 ]
    DEBUG: Module 1, diagnostic code 234 [ “liselect” 16 ]
    DEBUG: Module 1, diagnostic code 234 [ “litree” 47 ]
    DEBUG: Module 1, diagnostic code 234 [ “ligeoout” 59 ]
    DEBUG: Module 1, diagnostic code 234 [ “livrml” 67 ]
    DEBUG: Module 1, diagnostic code 234 [ “liphoto” 66 ]
    DEBUG: Module 1, diagnostic code 234 [ “liscrbuf” 69 ]
    DEBUG: Module 1, diagnostic code 234 [ “lishpost” 68 ]
    DEBUG: Module 1, diagnostic code 234 [ “licolour” 81 ]
    DEBUG: Module 1, diagnostic code 234 [ “liqtvr” 80 ]
    DEBUG: Module 1, diagnostic code 234 [ “limeshio” 70 ]
    DEBUG: Module 1, diagnostic code 234 [ “lifilter” 98 ]
    DEBUG: Module 1, diagnostic code 234 [ “litexgen” 102 ]
    DEBUG: Module 1, diagnostic code 234 [ “liiir” 114 ]
    DEBUG: Module 1, diagnostic code 234 [ “liesslte” 124 ]
    DEBUG: Module 1, diagnostic code 234 [ “liogldrv” 116 ]
    INFO: Module 5, diagnostic code 532
    INFO: Module 5, diagnostic code 532
    INFO: Module 5, diagnostic code 533
    INFO: Module 5, diagnostic code 533
    DEBUG: Module 1, diagnostic code 234 [ “liopengl” 111 ]
    DEBUG: Module 1, diagnostic code 234 [ “liglutil” 142 ]
    DEBUG: Module 1, diagnostic code 234 [ “lisketch” 130 ]
    DEBUG: Module 1, diagnostic code 234 [ “linpr” 96 ]
    DEBUG: Module 1, diagnostic code 234 [ “liobject” 112 ]
    DEBUG: Module 1, diagnostic code 234 [ “liiomgr” 149 ]
    DEBUG: Module 1, diagnostic code 234 [ “lirpc” 147 ]
    DEBUG: Module 1, diagnostic code 234 [ “lirenout” 132 ]
    DEBUG: Module 1, diagnostic code 234 [ “lidrvepx” 131 ]
    DEBUG: Module 1, diagnostic code 234 [ “licrypto” 152 ]
    DEBUG: Module 1, diagnostic code 234 [ “liplgdb” 158 ]
    DEBUG: Module 1, diagnostic code 234 [ “liptstor” 161 ]
    DEBUG: Module 1, diagnostic code 234 [ “liirad” 163 ]
    DEBUG: Module 1, diagnostic code 234 [ “lilic” 21 ]
    DEBUG: Module 1, diagnostic code 234 [ “lisysutl” 167 ]
    DEBUG: Module 1, diagnostic code 234 [ “lisesman” 23 ]
    DEBUG: Module 1, diagnostic code 234 [ “lismifac” 169 ]
    DEBUG: Module 1, diagnostic code 234 [ “lidrvwpp” 78 ]
    DEBUG: Module 1, diagnostic code 203
    These entries appear when ApplicationControl.Terminate is invoked:
    DEBUG: Module 1, diagnostic code 259 [ “lidrvwin” 30 ]
    DEBUG: Module 1, diagnostic code 259 [ “lidriver” 26 ]
    DEBUG: Module 1, diagnostic code 259 [ “lisystem” 1 ]
    This wasn’t doing this at all until I started trying to configure (x86)/(x64) releases, and at this point I’m completely stumped. Thanks for any insight you might be able to provide.

  2. Allockse Avatar
    Allockse

    Looks like I got it partially figured out. I’m using the x64 bit references of Autodesk.Navisworks.Api.dll and Autodesk.Navisworks.Controls.dll. My project’s active configuration was set to x64, however setting the platform target in Visual Studio to Any CPU, this debuging information is present.
    Setting the Platform Target back to x64 works while debugging, however as soon as I call my exe from a .bat file the same debugging information appears in the console.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading