2 Replies Latest reply on May 28, 2014 11:08 PM by Ron Bates

    C# CloseDoc Crash

    Ron Bates

      I have a SW file opened assigned as ModelDoc2 swModel.


      I do some stuff, and ultimately save it as x_t, then load that x_t with:


      ModelDoc2 swModelImported = iSwApp.LoadFile4(tempfile, "i", null, errors);


      I then copy the imported bodies to my original swModel as follows:


                swFeatMgrImported = (FeatureManager)swModelImported.FeatureManager;

                swFeatImported = (Feature)swModelImported.FirstFeature();


                  while ((swFeatImported != null))


                      FeatType = swFeatImported.Name;

                      FeatTypeName = swFeatImported.GetTypeName2();

                      if (FeatTypeName == "SolidBodyFolder" || FeatTypeName == "SurfaceBodyFolder")


                          swBodyFolder = (BodyFolder)swFeatImported.GetSpecificFeature2();

                          Bodies = (object[])swBodyFolder.GetBodies();

                          for (int bodyid = 0; bodyid <= (swBodyFolder.GetBodyCount() - 1); bodyid++)


                              swBody = (Body2)Bodies[bodyid];

                              swPart = (PartDoc)swModel;

                              swFeat = (Feature)swPart.CreateFeatureFromBody3(swBody, false, (int)swCreateFeatureBodyOpts_e.swCreateFeatureBodyCheck);



                      swFeatImported = (Feature)swFeatImported.GetNextFeature();



      Everything works fine.  But technically, after this, I'm essentially done with the imported file.  So I want to close it:





      From there, my original swModel is in a bad state (graphics area dead) and most of the time (but not always!?) SW will then crash.


      If I avoid actually copying the solid/surface bodies from the imported file, everything is fine, and I can close the imported file OK. 


      So I assume it's some sort of cleanup i must be missing after I complete the CreateFeatureFromBody3.  But it's unclear to me what it is.


      Any ideas?

        • Re: C# CloseDoc Crash
          Artem Taturevych

          There are two possible issue I can see which potentially may cause such problems:


          a) You need to use temporary body to create a feature from body. In your code you are using regular body. So change to something like that:


          swBody = (Body2)Bodies[bodyid];

          IBody2 tempBody = swBody.Copy() as IBody2;

          and use the temp body in this method:

          swFeat = (Feature)swPart.CreateFeatureFromBody3(tempBody, false, (int)swCreateFeatureBodyOpts_e.swCreateFeatureBodyCheck);


          b) Also it may be unsafe to create a feature from invisible part (in this case not active part). So may be worth to either firstly close the imported part (please note that the temp body you have now is no longer linked to original body so it is safe to close the model) or just activate the main model before like:

          swApp.ActivateDoc ((swPart as IModelDoc2).GetTitle());

          swFeat = (Feature)swPart.CreateFeatureFromBody3(tempBody, false, (int)swCreateFeatureBodyOpts_e.swCreateFeatureBodyCheck);


          Regards, Artem Taturevych | Snr. Developer | IC3D ANZ


          IC3DSteel – New Steel Solution for SolidWorks

          translationXpert – SolidWorks files language translator

          LinkedIn - SolidWorks API Group