Add an assembly into another assembly programatically |
Top Previous Next |
Here is the code to do that:
HRESULT AddAssemblyToAssemblyAtPosition (CComPtr<AssemblyComponentDefinition>& pParentAsmDef, const CString& kcsFullPathAsmName, const double kxPos, const double kyPos, const double kzPos, CComPtr<ComponentOccurrence>& pChildAsmOcc, CComQIPtr<AssemblyComponentDefinition>& pChildAsmDef, 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 */ { pChildAsmOcc = nullptr ;
CComPtr<ComponentOccurrences> pOccurrencesList = nullptr; HRESULT hRes = pParentAsmDef->get_Occurrences (&pOccurrencesList) ; if (FAILED(hRes)) { return ReturnAndShowCOMError (hRes,L"AddAssemblyToAssemblyAtPosition 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(kcsFullPathAsmName),pPosRotMatrix,&pChildAsmOcc) ; if (FAILED(hRes)) { return ReturnAndShowCOMError (hRes,L"AddAssemblyToAssemblyAtPosition Add failed") ; }
// Get the general component definition... CComQIPtr<ComponentDefinition> pCompDef ; hRes = pChildAsmOcc->get_Definition (&pCompDef) ; if (FAILED(hRes)) { return ReturnAndShowCOMError (hRes,L"AddAssemblyToAssemblyAtPosition could not get child definition.") ; }
// Cast from the general sort of component definition to the specific assembly component definition pChildAsmDef = CComQIPtr<AssemblyComponentDefinition>(pCompDef); if (pChildAsmDef == nullptr) { gLogger.Printf (ekErrMsg,L"AddAssemblyToAssemblyAtPosition(%s) could not cast to pChildAsmDef.") ; return E_FAIL ; }
return S_OK ; }
Enter topic text here. |
Text, images and diagrams © 2021 Owen F. Ransen. All rights reserved. (But copy the source code as much as you want!)