if (mApplication->ActiveDocumentType == kPartDocumentObject)
{
//write your code here to collect surface bodies from part document.
}
else if (mApplication->ActiveDocumentType == kAssemblyDocumentObject)
{
//write your code here to collect surface bodies from Assembly document.
}
vector surfaceBodyList HRESULT hr;
CComPtr document; hr = mInvApplication->get_ActiveDocument(&document); CComQIPtr partDocument(document); CComPtr partCompDef; hr = partDocument->get_ComponentDefinition(&partCompDef); // Solid bodies CComPtrsurfaceBodies; hr = partCompDef->get_SurfaceBodies(&surfaceBodies); for (int count = 1; count <= surfaceBodies->Count; count++) { SurfaceBodyPtr surfaceBody; surfaceBodies->get_Item(count,&surfaceBody); surfaceBodyList.push_back(surfaceBody); } // Sheet bodies CComPtr workSurfaces; hr = partCompDef->get_WorkSurfaces(&workSurfaces); for (int index = 1; index <= workSurfaces->GetCount(); index++) { CComPtr workSurface; workSurfaces->get_Item(CComVariant( index),&workSurface); CComPtr tWorkSurfaceBodies; tWorkSurfaceBodies = workSurface->SurfaceBodies; for (int count = 1; count <= tWorkSurfaceBodies->Count; count++) { SurfaceBodyPtr workSurfaceBody; tWorkSurfaceBodies->get_Item(count,&workSurfaceBody); surfaceBodyList.push_back(workSurfaceBody); } }
if(mInvApplication->ActiveDocumentType==kAssemblyDocumentObject)
{
vector surfaceBodyList
AssemblyDocumentPtr assmDoc = NULL;
assmDoc = (AssemblyDocumentPtr) mInvApplication->ActiveDocument;
ComponentOccurrencesPtr componentOccurrences = NULL;
componentOccurrences = assmDoc->ComponentDefinition->Occurrences;
hr = getAssemblySurfaceBodie(componentOccurrences,surfaceBodyList);
}
void getAssemblySurfaceBodies(ComponentOccurrencesPtr inCompOccurrences, vector& outSurfaceBody)
{
for (int i = 1; i <= inCompOccurrences->Count; i++)
{
ComponentOccurrencePtr compOccurrence = inCompOccurrences->GetItem(i);
if(compOccurrence->GetSuppressed())
continue;
if(!compOccurrence->GetVisible())
continue;
// Use the Count property of the SubOccurrences property to determine if it's a subassembly or not.
ComponentOccurrencesPtr SubOccurrences = compOccurrence->SubOccurrences;
int SubOccurrencesCount = SubOccurrences->Count;
if (SubOccurrencesCount > 0)
{
// It's a subassembly
// Recursive call
getAssemblySurfaceBodies(SubOccurrences,outSurfaceBody);
}
else
{
// It's not a subassembly
SurfaceBodiesPtr surfaceBodies = compOccurrence->SurfaceBodies;
int bodyCount = surfaceBodies->Count;
for (int count = 1; count <= bodyCount; count++)
{
SurfaceBodyPtr surfaceBody = NULL;
surfaceBody = surfaceBodies->GetItem(count);
if(surfaceBody == NULL)
continue;
if(surfaceBody->GetVisible()){
outSurfaceBody.push_back(surfaceBody);
surfaceBodyMatrix.push_back(NULL);
}
}
ComponentDefinitionPtr componentDefinitionPtr;
componentDefinitionPtr = compOccurrence->GetDefinition();
ObjectTypeEnum objEnum = componentDefinitionPtr->GetType();
if(objEnum == kPartComponentDefinitionObject)
{
matrix = compOccurrence->GetTransformation();
// Surface bodies Triangulation
CComPtr workSurfaces;
PartComponentDefinitionPtr partComponentDefinition(componentDefinitionPtr);
hr = partComponentDefinition->get_WorkSurfaces(&workSurfaces);
for (int index = 1; index <= workSurfaces->GetCount(); index++)
{
CComPtr workSurface;
workSurfaces->get_Item(CComVariant( index),&workSurface);
CComPtr tWorkSurfaceBodies = workSurface->SurfaceBodies;
for (int count = 1; count <= tWorkSurfaceBodies->Count; count++)
{
SurfaceBodyPtr workSurfaceBody;
tWorkSurfaceBodies->get_Item(count,&workSurfaceBody);
if(workSurfaceBody->GetVisible())
{
outSurfaceBody.push_back(workSurfaceBody);
}
}
}
}
else if(objEnum == kSheetMetalComponentDefinitionObject)
{
// Surface bodies Triangulation
SheetMetalComponentDefinitionPtr SheetMetalComDef(componentDefinitionPtr);
CComPtr workSurfaces;
hr = SheetMetalComDef->get_WorkSurfaces(&workSurfaces);
for (int index = 1; index <= workSurfaces->GetCount(); index++)
{
CComPtr workSurface;
workSurfaces->get_Item(CComVariant( index),&workSurface);
CComPtr tWorkSurfaceBodies = workSurface->SurfaceBodies;
for (int count = 1; count <= tWorkSurfaceBodies->Count; count++)
{
SurfaceBodyPtr workSurfaceBody;
tWorkSurfaceBodies->get_Item(count,&workSurfaceBody);
if(workSurfaceBody->GetVisible())
{
outSurfaceBody.push_back(workSurfaceBody);
}
}
}
}
}
}
}
void getFacetFromSurfaceBodies( const vector& inSurfaceBody)
{
HRESULT hr = S_OK;
vector arrVertex;
vector arrIndex;
int currIndex=0;
for(int i=0;i<inSurfaceBody.size();i++)
{
SurfaceBodyPtr surfaceBody = inSurfaceBody[i];
//Determine the highest tolerance of the existing facet sets.
long toleranceCount;
SAFEARRAY* existingTol =NULL ;
double* pTols;
surfaceBody->GetExistingFacetTolerances(&toleranceCount,&existingTol);
long index = 0;
double bestTol=0.1;
hr = SafeArrayGetElement(existingTol,&index,&bestTol);
for (long i = 1; i < toleranceCount; i++)
{
double tol;
hr = SafeArrayGetElement(existingTol,&i,&tol);
if (tol < bestTol)
bestTol = tol;
}
//Get a set of existing facets.
long vertexCount;
long facetCount;
//Calculate Facets on each Surface Body
SAFEARRAY* vertexCoords = NULL;
SAFEARRAY* normalVectors = NULL;
SAFEARRAY* vertexIndices = NULL;
hr=surfaceBody->CalculateFacets(bestTol,&vertexCount,&facetCount,
&vertexCoords,&normalVectors,&vertexIndices);
int length;
long lBound;
long uBound;
unsigned int dim = SafeArrayGetDim(vertexCoords);
hr = SafeArrayGetLBound(vertexCoords,dim,&lBound);
hr = SafeArrayGetUBound(vertexCoords,dim,&uBound);
length = (uBound - lBound)+1;
size_t lastVSize = arrVertex.size();
arrVertex.resize(lastVSize+length);
//Get x,y,z coordinate of each vertex
float x,y,z;
for(long i=0;i<length;i+=3)
{
SafeArrayGetElement(vertexCoords,&i,&x);
arrVertex[lastVSize+i] = x;
i++;
SafeArrayGetElement(vertexCoords,&i,&y);
arrVertex[lastVSize+i] = y;
i++;
SafeArrayGetElement(vertexCoords,&i,&z);
arrVertex[lastVSize+i] = z;
}
//Get indices from index array to create facet
lastVSize = arrIndex.size();
arrIndex.resize(lastVSize+ (facetCount*3));
for(long i=0;i<facetCount*3;i++)
{
int val;
SafeArrayGetElement(vertexIndices,&i,&val);
arrIndex[lastVSize+i] = currIndex + val-1;
}
currIndex = arrVertex.size()/3;
}
return S_OK;
}