Constrain a sketch point to a coordinate

Top  Previous  Next

This function can be used to add sketch points in defined xy positions in a 2d sketch:

 

Grounded-Sketch-Origin

 

Note that AddTwoPointDistance uses the "initial" positions of the points to work out the distance and thus fix them with a dimension constraint.

 

 

// Given a sketch and a horizontal and vertical position this function adds two

// points, one at the origin of the sketch and the other constrained to the position

// in the parameters

void AddConstrainedPointWRTOrigin(CComPtr<PlanarSketch>& pNewSketch,

                                 const double kxmmPos,

                                 const double kymmPos)

{

   CComPtr<TransientGeometry> pTrGeom = GetTransGeomPtr();

 

   // This will be a point fixed (grounded) at the origin...

   CComPtr<Point2d> pOrigin2d;

   pTrGeom->CreatePoint2d(0.0,0.0,&pOrigin2d); 

   CComPtr<SketchPoint> pSketchOrigin;

   HRESULT hr = pNewSketch->SketchPoints->Add(pOrigin2d, VARIANT_FALSE, &pSketchOrigin);

 

   CComPtr<GeometricConstraints> pGeoCons;

   hr = pNewSketch->get_GeometricConstraints(&pGeoCons);

 

   // Fix the sketch point...

   CComQIPtr<SketchEntity> pSkEnt(pSketchOrigin);

   hr = pGeoCons->AddGround (pSkEnt); // !!!IMPORTANT!!! to make the origin fixed

 

   // Sketches work in cm so convert to cm from mm....

   const double kcmxPos = kxmmPos / 10.0;

   const double kcmyPos = kymmPos / 10.0;

 

   // The new point will be added to the sketch and constrained...

   CComPtr<Point2d> pNewPoint2d;

   pTrGeom->CreatePoint2d(kcmxPos,kcmyPos,&pNewPoint2d); 

   CComPtr<SketchPoint> pNewSketchPointA ;

   hr = pNewSketch->SketchPoints->Add(pNewPoint2d, VARIANT_FALSE, &pNewSketchPointA);

   if (FAILED(hr)) {

       gLogger.Printf(ekErrMsg, L"Could not add sketch point");

       return;

   }

 

   // Work out a good point for the text of the dimension

   double xText = kcmxPos * 0.9;

   double yText = kcmyPos / 2.0;

   CComPtr<Point2d> pVTextPoint;

   pTrGeom->CreatePoint2d(xText,yText,&pVTextPoint); 

 

   hr = pNewSketch->DimensionConstraints->AddTwoPointDistance(pSketchOrigin,

                                                              pNewSketchPointA,

                                                              DimensionOrientationEnum::kVerticalDim,

                                                              pVTextPoint,

                                                              VARIANT_FALSE);

   if (FAILED(hr)) {

       ShowCOMError(hr, L"Could not add constraint AddTwoPointDistance vertical");

       return;

   }

 

   xText = kcmxPos / 2.0 ;

   yText = kcmxPos * 0.9 ;

   CComPtr<Point2d> pHTextPoint;

   hr = pTrGeom->CreatePoint2d(xText,yText,&pHTextPoint); 

   if (FAILED(hr)) {

       ShowCOMError(hr, L"Could not add CreatePoint2d");

       return;

   }

 

   hr = pNewSketch->DimensionConstraints->AddTwoPointDistance(pSketchOrigin,

                                                              pNewSketchPointA,

                                                              DimensionOrientationEnum::kHorizontalDim,

                                                              pHTextPoint,

                                                              VARIANT_FALSE);

   if (FAILED(hr)) {

       ShowCOMError(hr, L"Could not add constraint AddTwoPointDistance horizontal");

       return;

   }

}

 

 

text here.

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