How to extrude a sketch programatically

Top  Previous  Next

Here is a function which takes five parameters and creates a 3D extruded feature in the part. The feature could be a hole, as well as normal solid objects....

 

Extrusion-with-kCutOperation

 

 

HRESULT ExtrudeSketch (const wchar_t* const pszExtrusionName,    // name of created feature

                      const double kHeightMm,

                      const wchar_t* const pszSketchName,        // which sketch to extrude

                      const PartFeatureOperationEnum eOperation, // how to extrude

                      const PartFeatureExtentDirectionEnum eDirection,

                      CComPtr<PartComponentDefinition>& pPartCompDef)

/*

Given a height and a sketch name the sketch is extruded to the given height.

The resultant feaure is placed inside pPartCompDef, and is called pszExtrusionName

eOperation tells us whether to cut a hole or create a solid object etc.

eDirection tells us which direction to make the extrusion.

*/

{

   // Get the sketch in the part by name...

   CComPtr<PlanarSketch> pSketch;

   HRESULT hRes = pPartCompDef->Sketches->get_Item(CComVariant(pszSketchName), &pSketch);

   if (FAILED(hRes) || (pSketch == nullptr)) {

       return ReturnAndShowCOMError (hRes,L"ExtrudeSketch but could not get sketch by name\n");

   }

 

   CComPtr<Profile> pProfile ; // This is the return value of the call to AddForSolid

                               // and will be used when we extrude.

   CComVariant pSegs;          // not used

   CComVariant pReserve;       // not used

   hRes = pSketch->Profiles->AddForSolid(VARIANT_TRUE,pSegs,pReserve,&pProfile);

   if (FAILED(hRes) || (pProfile == nullptr)) {

       return ReturnAndShowCOMError (hRes,L"ExtrudeSketch but could not AddForSolid for pProfile\n");

   }

 

   // Features are 3D objects. Get the list of them so we can add to that list...

   CComPtr<PartFeatures> pListOfFeatures;

   hRes = pPartCompDef->get_Features(&pListOfFeatures);

   if (FAILED(hRes) || (pListOfFeatures == nullptr)) {

       return ReturnAndShowCOMError (hRes,L"ExtrudeSketch but could not get_Features\n");

   }

 

   // Get the list of *extruded* features...

   CComPtr<ExtrudeFeatures> pListOfExtrusions;

   hRes = pListOfFeatures->get_ExtrudeFeatures(&pListOfExtrusions);

   if (FAILED(hRes) || (pListOfExtrusions == nullptr)) {

       return ReturnAndShowCOMError (hRes,L"ExtrudeSketch but could not get_ExtrudeFeatures\n");

   }

 

   // Now at last we can use the pProfile to create a new extrusion, adding it to the list

   // of extruded features. This is where we use the eOperation parameter

   CComPtr<ExtrudeDefinition> pExtrudeDef;

   hRes = pListOfExtrusions->CreateExtrudeDefinition(pProfile,eOperation,&pExtrudeDef);

   if (FAILED(hRes) || (pExtrudeDef == nullptr)) {

       return ReturnAndShowCOMError (hRes,L"ExtrudeSketch but could not CreateExtrudeDefinition\n");

   }

 

   // Specify the distance and direction of the extrustion...

   // This function needs it in cm, so convert from mm to cm

   pExtrudeDef->SetDistanceExtent(_variant_t(kHeightMm/10.0),eDirection);

 

   // Add the extrusion.

   CComPtr<ExtrudeFeature> pExtrude;

   hRes = pListOfExtrusions->Add (pExtrudeDef,&pExtrude);

   if (FAILED(hRes) || (pExtrude == nullptr)) {

       return ReturnAndShowCOMError (hRes,L"ExtrudeSketch but could Add ExtrudeDefinition\n");

   }

 

 

   pExtrude->put_Name (CComBSTR (pszExtrusionName)) ;

 

   return (hRes) ;

}

 

 

Here are the possible values for the two enumerators used above:

 

PartFeatureExtentDirectionEnum



Enumerator

Value

Description

kNegativeExtentDirection

20994

Extension in the negative direction.

kPositiveExtentDirection

20993

Extension in the positive direction

kSymmetricExtentDirection

20995

Extension in positive AND negative direction.

 

 

PartFeatureOperationEnum



Enumerator

Value

Description

kCutOperation

20482

Cut operation. (i.e. make a hole)

kIntersectOperation

20483

Intersect operation.

kJoinOperation

20481

Join operation.

kNewBodyOperation

20485

New Body operation.

kSurfaceOperation

20484

Surface operation.

 

 

This image has used kCutOperation and kSymmetricExtentDirection around a center plane of a cylinder, the sketch contained a single circle:

 

 

Extrusion-with-kCutOperation

 

 

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