5 Replies Latest reply on Oct 1, 2013 8:11 AM by Mahesh Revanwar

    Retrieve information from IModelDoc2

    Mahesh Revanwar

      Hello,

       

      I've opened a .sldprt file using API "ISldWorks::OpenDoc6()" and got IModelDoc2** as an output.

      Please tell me, how to retrieve sketch information such as entities, constraints, dimensions from that model doc?

       

      Thanks and regards

        • Re: Retrieve information from IModelDoc2
          Keith Rice

          Mahesh,

           

          Since it sounds like you are new SolidWorks API I would recommend watching some of the free videos here so you can get comfortable with using the API Help.

           

          You will need to traverse all of the features in the model using IModelDoc2::FirstFeature and IFeature::GetNextFeature. Use IFeature::GetTypeName2 to determine if the feature is a sketch. If it is, use IFeature::GetSpecificFeature2 to access ISketch. Then you can use ISketch::GetSketchSegments to get sketch arcs, lines, ellipses, parabolas, or splines.

           

          You'll need to cast from each of those objects to the more specific object (like ISketchLine) for example and then use members within ISketchLine to obtain information about the sketch entities. For example of how some of this works, see the API Help example "Find Total Length of Sketch Segments in Sketch Example (VBA)".

           

          If you want to get all of the dimensions in a sketch, you will need to turn on the feature dimensions display filter and then use IFeature::GetFirstDisplayDimension and IFeature::GetNextDisplayDimension to traverse all of the dimensions. To traverse relations you can use ISketchSegment::GetRelations.

           

          This isn't an easy macro to write if you are new to the API so I would recommend you learn how to use the API Help well. You might also benefit from reading this article: 7 Mistakes New SolidWorks API Programmers Make

           

          Keith

          SolidWorks API Training

            • Re: Retrieve information from IModelDoc2
              Mahesh Revanwar

              Thanks Keith.

               

              But this is partially working.

              I have imported .sldprt file which is created as a new part with circle as a sketch entity and saved it, but I am not getting ISketch from IFeature::GetSpecificFeature2. And IFeature::GetTypeName2 gives SensorFolder as string. Is it right?

               

              Thanks & regards,

                • Re: Retrieve information from IModelDoc2
                  Mahesh Revanwar

                  Hello,

                   

                  I could able to get ISketch** which is associated with a feature but failed to get ISketch** from an isolated sketch.

                   

                  Here is the code I have written:

                   

                       m_iSldWorks->OpenDoc6(file1, swDocPART, swOpenDocOptions_LoadModel, (BSTR)"", &errors, &warnings, &model1);

                   

                       ISketchManager* sketchMngr = 0;

                       model1->get_SketchManager(&sketchMngr);

                       IModelDocExtension* extn = 0;

                       model1->get_Extension(&extn);

                       VARIANT_BOOL retVal = FALSE;

                       HRESULT hr = S_FALSE;

                       hr = extn->SelectByID2((BSTR)"Sketch1", (BSTR)"SKETCH", 0, 0, 0, FALSE, 0, NULL, 0, &retVal);

                       sketchMngr->InsertSketch(FALSE);

                   

                       ISketch* iSketch = 0;

                       sketchMngr->get_ActiveSketch(&iSketch);

                   

                  Please tell me is there any way to get ISketch** from an isolated sketch part file.

                   

                  For Ex. If I create a sketch, extrude it and saved as .sldprt then it gives ISketch** , but if I create a skech only and saved it as .sldprt then it doesn't give ISketch**.

                  • Re: Retrieve information from IModelDoc2
                    Santosh Pawar

                    Hi Mahesh,

                     

                    Are you traversing the features as suggested by Keith.

                    The Sensorfolder is the first feature in the featuremanager tree, you will have to traverse through all the features in the featuremanager tree to get to the Sketch and see IFeature::GetTypeName2 to determine if the feature is a sketch.

                    Also the in the code that you have submitted after the call to SelectByID2 you should be using SelectionMgr::GetSelectedObject6 to get the selected feature from this you can get the Sketch object by using swFeat.GetSpecificFeature2