Show a dimension programatically

Top  Previous  Next

This function will try to show a list of dimensions, when each dimension is based on a parameter.

 

 

And the definition is this (no error checking):

 

 

void ShowNamedRetDimsInView (const CString& kcsViewName,

                            const CStringList& kcsParamNames,

                            CComQIPtr<DrawingDocument>& pIDWDoc)

{

   // Find the sheets of the drawing document...

   CComPtr<Sheets> pSheets = nullptr ;

   HRESULT hRes = pIDWDoc->get_Sheets(&pSheets) ;

 

   // Get the (assumed) single sheet inside the IDW. Remember the first object

   // in a COM list had index 1...

   CComPtr<Sheet> pSheet = nullptr ;

   hRes = pSheets->get_Item (CComVariant(1),&pSheet) ;

 

   CComPtr<DrawingView> pThisView = nullptr ;

   const bool kbViewOk = FindDrawingViewInSheetByName (pThisView,pSheet,kcsViewName) ;

   if (!kbViewOk) {

       gLogger.Printf (ekErrMsg,

                       L"SNRDIV2, could not find view <%s>.",

                       kcsViewName);

       return ;

   }

 

   CComPtr<DrawingDimensions> pDimensions = nullptr ;

   hRes = pSheet->get_DrawingDimensions (&pDimensions) ;

 

   // The general dimensions gives you a way of getting at the retrievable dimensions....

   CComPtr<GeneralDimensions> pGenDimensions = nullptr ;

   hRes = pDimensions->get_GeneralDimensions (&pGenDimensions) ;

 

   // The Arrange function requires an object collection of dimensions to arrange...

   CComPtr<TransientObjects> pTransientObjects = GetTransientObjectsPtr() ;

   CComPtr<ObjectCollection> pObjCollection = nullptr ;

   hRes = pTransientObjects->CreateObjectCollection (gkvarEmpty,&pObjCollection) ;

   if (FAILED(hRes)) {

       ShowCOMError (hRes,L"CreateObjectCollection failed ");

       return;

   }

 

   // Retrieve *all* retrievable dimensions. This will make all the dimensions appear in the drawing...

   CComPtr<GeneralDimensionsEnumerator> pGenDimsEnum;

   hRes = pGenDimensions->Retrieve(pThisView, CComVariant(), &pGenDimsEnum);

 

   long nTotalDims=0;

   hRes = pGenDimsEnum->get_Count(&nTotalDims);

 

   // Go over all the dimensions, erasing those not in my list...

   for(int nDim = 1; nDim <= nTotalDims; nDim++)

   {

       CComPtr<GeneralDimension> pGenDim;

       hRes = pGenDimsEnum->get_Item(_variant_t(nDim), &pGenDim);

 

       CComPtr <DimensionText> pDimText;

       pGenDim->get_Text(&pDimText);

 

       CComBSTR DimText;

       pDimText->get_Text(&DimText);

 

       CComBSTR paramName;

       IDispatchPtr pGenDimIDisp;

       hRes = pGenDim->get_RetrievedFrom(&pGenDimIDisp);

 

       CComQIPtr<FeatureDimension> pFeatureDim = CComQIPtr<FeatureDimension>(pGenDimIDisp);

       if(pFeatureDim != NULL)

       {

           CComPtr<Parameter> pParam;

           hRes = pFeatureDim->get_Parameter(&pParam);

           hRes = pParam->get_Name(&paramName);

       }

 

       CComQIPtr<DimensionConstraint> pDimConstraint = CComQIPtr<DimensionConstraint>(pGenDimIDisp);

       if(pDimConstraint != NULL)

       {

           CComPtr<Parameter> pParam;

           hRes = pDimConstraint->get_Parameter(&pParam);

           hRes = pParam->get_Name(&paramName);

       }

 

       CComQIPtr<FeatureDimensionProxy> pFeatureDimProxy = CComQIPtr<FeatureDimensionProxy>(pGenDimIDisp);

       if(pFeatureDimProxy != NULL)

       {

           CComPtr<Parameter> pParam;

           hRes = pFeatureDimProxy->get_Parameter(&pParam);

           hRes = pParam->get_Name(&paramName);

       }

 

       /*

       * We will delete this dimension if it is NOT in the list

       * of dimensions we want.

       */

       bool bDelete = true ;

       for (INT_PTR iParam = 0 ; iParam < kcsParamNames.GetCount (); iParam++) {

           const POSITION Pos = kcsParamNames.FindIndex (iParam) ;

           const CString kcsListed = kcsParamNames.GetAt (Pos) ;

           if (wcscmp (kcsListed.GetString(),paramName) == 0) {

               // This parameter, paramName, is in the list, so I don't delete it

               bDelete = false ;

               break ;

           }

       }

 

       if (bDelete) {

           // This parameter is not one we want to retain...

           pGenDim->Delete();

 

       } else {

           // Add to the collection of dimensions to be arranged later...

           pObjCollection->Add(pGenDim);

       }

   }

 

 

   if (pObjCollection->Count > 0) {

       pDimensions->Arrange(pObjCollection);

   }

}

 

You can call the function in this way:

 

       CStringList csNames;

       csNames.AddHead(L"d147"); // This corresponds to the length of the collettore

       csNames.AddHead(L"Extra");

       csNames.AddHead(L"Diameter7");

       ShowNamedRetDimsInView (L"Front",csNames,pDrawingDoc) ;

 

 

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