Add a rectangle to a sketch

Top  Previous  Next

Here's a function to add a rectangle to a sketch:

 

bool AddRectToSketch (CComPtr<PlanarSketch>& pSketch,

                      const double kxMin,

                      const double kyMin,

                      const double kxMax,

                      const double kyMax) 

{

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

    CComPtr<TransientGeometry> pTrGeom = GetTransGeomPtr() ;

 

    CComPtr<Point2d> pMinPt; // Pure geometric point

    HRESULT hRes = pTrGeom->CreatePoint2d(kxMin,kyMin,&pMinPt);   

 

    CComPtr<Point2d> pMaxPt; // Pure geometric point

    hRes = pTrGeom->CreatePoint2d(kxMax,kyMax,&pMaxPt);

 

    // Create sketch points from above points

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

    CComPtr<SketchPoints> pSkPoints;

 

    hRes = pSketch->get_SketchPoints(&pSkPoints);

 

    // Create a SketchPoint from a geometric point 

    CComPtr<SketchPoint> pSpt1;

    hRes = pSkPoints->Add(pMinPt,           // geometrical point

                          VARIANT_FALSE, // not a hole center

                          &pSpt1);       // resulting SketchPoint

 

    CComPtr<SketchPoint> pSpt2;

    hRes = pSkPoints->Add(pMaxPt,VARIANT_FALSE,&pSpt2);

    CComPtr<SketchEntitiesEnumerator> pRectangleLines;

 

    // Get SketchLines

    CComPtr<SketchLines> pSkLines;

    hRes = pSketch->get_SketchLines(&pSkLines);

 

    // Creat a rectangle from two points

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

                                            _variant_t((IDispatch *)pSpt2),

                                            &pRectangleLines);

 

    return SUCCEEDED(hRes) ;

}

 

 

 

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