2 Replies Latest reply on Jul 1, 2015 9:44 PM by Joshua Jiang

    How get the Sketch Using SelectById2?

    Joshua Jiang

      I want to write a small programme that can automatically draws the part. The problem now is that I don't know how to use Holewizard to add several holes at one time.

      I try to preselect a point on the face, then use holewizard to create a hole, then edit the sketch and add the rest of the hole.(I wonder if there is a better way to do this.)

      Suppose I have a part file below.The arrow is the only point in the Sketch3.

      pic.bmp

      The following is my code (I use Vs2008 C++  to develop sw2012):

      CoInitialize(NULL); // initialize COM
      CComPtr<ISldWorks> swApp;
      HRESULT hres = swApp.CoCreateInstance(__uuidof(SldWorks), NULL, CLSCTX_LOCAL_SERVER);//CLSCTX_LOCAL_SERVER
      swApp->put_Visible(VARIANT_TRUE);
      swApp->put_UserControl(VARIANT_TRUE);
      {
      CComPtr<IModelDoc2> swDoc=NULL;
      CComPtr<IModelDocExtension> swDocExt=NULL;
      CComPtr<IFeatureManager> swFeatMgr=NULL;
      CComPtr<ISketchManager> swSketchMgr=NULL;
      VARIANT_BOOL bRetbool;
      swApp->get_IActiveDoc2(&swDoc);
      swDoc->get_Extension(&swDocExt);
      swDoc->get_FeatureManager(&swFeatMgr);
      swDoc->get_SketchManager(&swSketchMgr);
      hres = swDocExt->SelectByID2(L"Sketch3",L"SKETCH",0.0,0.0,0.0,VARIANT_FALSE,0,NULL,swSelectOptionDefault,&bRetbool);

      // This doesn't work. Cause it's invisible? Or I made other mistakes? Even if I specify the type argument as "SKETCH".,finally

      the Origin Point is selected. 

      swDoc->EditSketch();

      //hres = swDocExt->SelectByID2(L"Point1@Sketch3",L"EXTSKETCHPOINT",1.13,1.25,0.0,VARIANT_FALSE,0,NULL,swSelectOptionDefault,&bRetbool);
      //This sentence may work if I manually show the hiding Sketch3. But I don't think it's a good way.

       

       

      Thanks for any suggestion in advance!

        • Re: How get the Sketch Using SelectById2?
          Rajat Jain

          Hi..

           

          Since you already know the name of the Sketch (you are using it in SelectByID2), you can simply use PartDoc::FeatureByName("Sketch3") to get the feature object and then use feature.getSpecificFeature2() to get the underlying Sketch object.

           

          In your case, you can simply use Feature::Select2() to select the feature and ModelDoc::EditSketch() to edit the sketch.

           

          Hope it helps!!!

           

          Rajat Jain

            • Re: How get the Sketch Using SelectById2?
              Joshua Jiang

              Thanks for your reply and that helps me a lot. I always  try not using the obsolete APIs because I don't know whether they will cause something unexpected.

              Now I found the problem in my code. The string argument in my function should be BSTR. Although use L"XXXX" can initialize the string, that is not a correct way.

              Using the constructor by CComBSTR can solve the problem.

              Your suggestion offers me a better way. Thx!