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....
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:
This image has used kCutOperation and kSymmetricExtentDirection around a center plane of a cylinder, the sketch contained a single circle:
|
Text, images and diagrams © 2021 Owen F. Ransen. All rights reserved. (But copy the source code as much as you want!)