0

This is what solidworks tells you to do to reference swApp, but I keep getting NullReferenceException on the line that it is referenced.

Does anyone have any idea why? or How I go about repairing?

Can I reference it later on such as swApp = ????

private void button5_Click(object sender, EventArgs e)
{
    //Save Drawing
    ModelDoc2 swDoc = null;
    int longstatus = 0;
    swDoc = ((ModelDoc2)(swApp.ActiveDoc));
    longstatus = swDoc.SaveAs3(
            @"C:\Engineering\Engineering\SW Automation\Linear Actuator Technology\MLD Series\Prints\Configured Prints\" + 
            textBox1.Text + ".SLDDRW", 0, 2);
}

public SldWorks swApp;
Russell Saari
  • 985
  • 11
  • 26
  • 41

2 Answers2

2

Figured it out thanks for the help everyone.

SldWorks swApp = null;
swApp = (SldWorks)Activator.CreateInstance(
            Type.GetTypeFromProgID("SldWorks.Application"));
Devendra D. Chavan
  • 8,871
  • 4
  • 31
  • 35
Russell Saari
  • 985
  • 11
  • 26
  • 41
1

Somewhere you're going to have to populate swApp. I don't use SolidWorks so I can't give you the exact code but look in the constructor of their example code. Maybe you can find an example of how swApp is populated there.

Matt Cofer
  • 2,972
  • 1
  • 20
  • 18
  • yeah I used their macro recorder to record most of what is going on and the give you public SldWorks swApp; which does not help me in winforms. I think I will have to figure a way to define it each time I use it. – Russell Saari Sep 14 '11 at 18:10
  • 1
    That is what I generally do with NX. You access the Session first then get the work part. Terminology may be different in SolidWorks. When I use the journal recorder it shows how those variables are initialized though. – Matt Cofer Sep 14 '11 at 18:17