Add a part to an assembly |
Top Previous Next |
...or more properly how to add an occurrence of a part to an assembly. Here you go, and you specify the 3d position and optionally the 3d angles too...
HRESULT AddPartToAssemblyAtPosition (CComPtr<AssemblyComponentDefinition>& pAsmCompDef, const CString& kcsFullPathPartName, const double kxPos, const double kyPos, const double kzPos, CComPtr<ComponentOccurrence>& pPartOcc, CComQIPtr<PartComponentDefinition>& pPartCompDef, const double kxRotDegs /*=0.0*/, const double kyRotDegs /*=0.0*/, const double kzRotDegs /*=0.0*/) /* The positions are in the natural units of Inventor, usually cm We return the occurence and its part component definition */ { pPartOcc = nullptr ;
CComPtr<ComponentOccurrences> pOccurrencesList = nullptr; HRESULT hRes = pAsmCompDef->get_Occurrences (&pOccurrencesList) ; if (FAILED(hRes)) { return ReturnAndShowCOMError (hRes,L"AddPartToAssemblyAtPosition get_Occurrences failed") ; }
CComPtr<TransientGeometry> pTransGeom = GetTransGeomPtr () ; CComPtr<Matrix> pPosMatrix; pTransGeom->CreateMatrix(&pPosMatrix);
CComPtr<Vector> pVector; // Create a Vector to modify the Matrix pTransGeom->CreateVector(kxPos,kyPos,kzPos,&pVector);
// Make the matrix a "move to point" matrix... pPosMatrix->SetTranslation (pVector,VARIANT_TRUE) ;
CComPtr<Matrix> pPosRotMatrix ; pTransGeom->CreateMatrix(&pPosRotMatrix);
CComPtr<Vector> pZAxis ; hRes = pTransGeom->CreateVector (0,0,1,&pZAxis); if (FAILED(hRes)) { ShowCOMError (hRes,L"Could not create z axis vector") ; return false ; }
CComPtr<Point> pOrigin ; hRes = pTransGeom->CreatePoint (0,0,0,&pOrigin); const double kZRadsRot = RadsFromDegs(kzRotDegs); pPosRotMatrix->SetToRotation (kZRadsRot,pZAxis,pOrigin) ;
pPosRotMatrix->PreMultiplyBy(pPosMatrix);
hRes = pOccurrencesList->Add (CComBSTR(kcsFullPathPartName),pPosRotMatrix,&pPartOcc) ; if (FAILED(hRes)) { return ReturnAndShowCOMError (hRes,L"AddPartToAssemblyAtPosition Add failed") ; }
// Get the general component definition... CComQIPtr<ComponentDefinition> pCompDef ; hRes = pPartOcc->get_Definition (&pCompDef) ; if (FAILED(hRes)) { return ReturnAndShowCOMError (hRes,L"AddPartToAssemblyAtPosition could not get definition.") ; }
// Cast from the general sort of component definition to the specific PART component definition pPartCompDef = CComQIPtr<PartComponentDefinition>(pCompDef); if (pPartCompDef == nullptr) { gLogger.Printf (ekErrMsg,L"AddPartToAssemblyAtPosition could not cast to pPartCompDef.") ; return E_FAIL ; }
return S_OK ; }
|
Text, images and diagrams © 2021 Owen F. Ransen. All rights reserved. (But copy the source code as much as you want!)