Rectangular cut with extrusion

Top  Previous  Next

Here is what I use, complete with optional participating occurrences ObjectCollection:

 

bool ExtrudeSketchInAsm (const wchar_t* const pszExtrusionName,    // name of created feature

                        const double kHeight,

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

                        const PartFeatureOperationEnum eOperation, // how to extrude

                        const PartFeatureExtentDirectionEnum eDirection,

                        CComPtr<AssemblyComponentDefinition>& pAsmCompDef,

                        CComPtr<ObjectCollection> pParticipatingObjects /* = nullptr*/)

/*

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

The resultant feature is placed inside pAsmCompDef, 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.

If pParticipatingObjects is nullptr all occurrences in the assembly are affected.

If pParticipatingObjects is a list of occurrences ony thos occurrences are affected.

*/

{

   gLogger.Printf (ekLogMsg,L"ESIA extrusion called %s, height %.2fmm, sketch=%s\n",

                   pszExtrusionName,kHeight,pszSketchName) ;

 

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

   CComPtr<PlanarSketch> pSketch;

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

   if (FAILED(hRes)) {

       ShowCOMError (hRes,L"ESIA, but could not get sketch by name: <%s>\n",pszSketchName);

       return false ;

   }

 

   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)) {

       ShowCOMError (hRes,L"ESIA, but could not AddForSolid for pProfile\n");

       return false ;

   }

 

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

   CComPtr<Features> pListOfFeatures=nullptr;

   hRes = pAsmCompDef->get_Features(&pListOfFeatures);

   if (FAILED(hRes)) {

       ShowCOMError (hRes,L"ESIA, but could not get_Features\n");

       return false ;

   }

 

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

   CComPtr<ExtrudeFeatures> pListOfExtrusions;

   hRes = pListOfFeatures->get_ExtrudeFeatures(&pListOfExtrusions);

   if (FAILED(hRes)) {

       ShowCOMError (hRes,L"ESIA, but could not get_ExtrudeFeatures\n");

       return false ;

   }

 

   // 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)) {

       ShowCOMError (hRes,L"ESIA, but could not CreateExtrudeDefinition\n");

       return false ;

   }

 

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

   pExtrudeDef->SetDistanceExtent(_variant_t(kHeight),eDirection);

 

   if (pParticipatingObjects != nullptr) {

       hRes = pExtrudeDef->put_AffectedOccurrences(pParticipatingObjects);

       if (FAILED(hRes)) {

           ShowCOMError (hRes,L"ESIA, but could not put_AffectedOccurrences for <%s>\n",pszExtrusionName);

           return false ;

       }

   }

 

   // Add the extrusion.

   CComPtr<ExtrudeFeature> pExtrude = nullptr ;

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

   if (FAILED(hRes)) {

       ShowCOMError (hRes,L"ESIA, but could not Add ExtrudeDefinition for <%s>\n"

                     L"***LISTEN*** This can happen if the extrusion does not change the model.",pszExtrusionName);

       return false ;

   }

 

   pExtrude->put_Name (CComBSTR (pszExtrusionName)) ;

 

   return true ;

}

 

 

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