//Функция конвертирует Solid в CSMesh
void RvtConverter::ConvertSolidToCSMesh(Solid ^ pSolid, CSMesh ^% pMesh, bool isRebar)
{
CSVector3 pt1, pt2, pt3;
if (pMesh == nullptr) {
pMesh = gcnew CSMesh;
pMesh->Clear();
}//of if
FaceArrayIterator^ pFaceIt = pSolid->Faces->ForwardIterator();
pFaceIt->Reset();
//Цикл по граням
while (pFaceIt->MoveNext()) {
Face^ pFace = (Face^)(pFaceIt->Current);
Mesh^ pRevitMesh = nullptr;
CSMaterial^ material = nullptr;
Material^ pMat = dynamic_cast<Material^>(m_doc->GetElement(pFace->MaterialElementId));
if (pMat != nullptr)
material = gcnew CSMaterial(pMat->Color->Red, pMat->Color->Green, pMat->Color->Blue, 255-pMat->Transparency*255/100);
else
material = gcnew CSMaterial(128, 128, 128, 255);
pMesh->SetCurrentMaterial(material);
//Если передан арматурный элемент, уменьшаем густоту сетки триангуляции
if (isRebar) pRevitMesh = pFace->Triangulate(0.1);
else pRevitMesh = pFace->Triangulate();
UV^ uv = gcnew UV(0, 0);
XYZ^ pXYZNormal = pFace->ComputeNormal(uv);
CSVector3 NormalVec;
NormalVec.x = -1 * float(pXYZNormal->X);
NormalVec.y = -1 * float(pXYZNormal->Y);
NormalVec.z = -1 * float(pXYZNormal->Z);
for (int i = 0; i < pRevitMesh->NumTriangles; i++) {
MeshTriangle^ pTriangle = pRevitMesh->Triangle[i];
XYZ^ pVertex = pTriangle->Vertex[0];
pt1.x = float(pVertex->X*304.8);
pt1.y = float(pVertex->Y*304.8);
pt1.z = float(pVertex->Z*304.8);
pVertex = pTriangle->Vertex[1];
pt2.x = float(pVertex->X*304.8);
pt2.y = float(pVertex->Y*304.8);
pt2.z = float(pVertex->Z*304.8);
pVertex = pTriangle->Vertex[2];
pt3.x = float(pVertex->X*304.8);
pt3.y = float(pVertex->Y*304.8);
pt3.z = float(pVertex->Z*304.8);
pMesh->AddTriangleFace(pt3, pt2, pt1, true /*No Normal*/);
int LastIndx = pMesh->Index->Count - 1;
if (LastIndx >= 2) {
pMesh->Vertices[pMesh->Index[LastIndx]]->SetNormal(NormalVec);
pMesh->Vertices[pMesh->Index[LastIndx - 1]]->SetNormal(NormalVec);
pMesh->Vertices[pMesh->Index[LastIndx - 2]]->SetNormal(NormalVec);
}
}// of for
}//of while
}