Adding a rectangle to a sketch programatically

Top  Previous  Next

We create a rectangle in a 2D sketch by specifying 2 points. You get the points from transient geometry object, but they need to be used to create the two SketchPoint things.

 

   // Get the transient geometry object. We'll use this to create pure geometric points

   CComPtr<TransientGeometry> pTrGeom;

   pTrGeom = pApp->TransientGeometry;

 

   // Draw a 4cm x 3cm rectangle with the corner at (0,0)

   CComPtr<Point2d> pPt1; // Pure geometric point

   hr = pTrGeom->CreatePoint2d(0.0,0.0,&pPt1);   

   if (FAILED(hr))

       return ReturnAndShowCOMError (hr,L" create 2d point failed") ; 

 

   CComPtr<Point2d> pPt2; // Pure geometric point

   hr = pTrGeom->CreatePoint2d(4.0,3.0,&pPt2);

   if (FAILED(hr))

       return ReturnAndShowCOMError (hr,L" create 2d point failed") ; 

 

   // Create sketch points from above points

   // Get list of SketchPoints, points existing in a sketch

   CComPtr<SketchPoints> pSkPoints;

   hr = pSketch->get_SketchPoints(&pSkPoints);

   if (FAILED(hr))

       return ReturnAndShowCOMError (hr,L" get sketch point list failed") ; 

 

   // Create a SketchPoint from a geometric point

   CComPtr<SketchPoint> pSpt1;

   hr = pSkPoints->Add(pPt1,           // geometrical point

                       VARIANT_FALSE,  // not a hole center

                       &pSpt1);        // resulting SketchPoint

   if (FAILED(hr))

       return ReturnAndShowCOMError (hr,L" add point to sketch failed") ; 

 

   CComPtr<SketchPoint> pSpt2;

   hr = pSkPoints->Add(pPt2,VARIANT_FALSE,&pSpt2);

   if (FAILED(hr))

       return ReturnAndShowCOMError (hr,L" add point to sketch failed") ; 

 

   CComPtr<SketchEntitiesEnumerator> pRectangleLines;

 

   // Get SketchLines

   CComPtr<SketchLines> pSkLines;

   hr = pSketch->get_SketchLines(&pSkLines);

   if (FAILED(hr))

       return ReturnAndShowCOMError (hr,L" get lines list failed") ; 

 

   TRACE (L"There are currently %d lines in the sketch\n",pSkLines->Count) ;

 

   // Creat a rectangle from two points

   hr = pSkLines->AddAsTwoPointRectangle(_variant_t((IDispatch *)pSpt1),

                                         _variant_t((IDispatch *)pSpt2),

                                         &pRectangleLines);

 

 

 

 

 

 

Text, images and diagrams © 2021 Owen F. Ransen. All rights reserved. (But copy the source code as much as you want!)