#include "StdAfx.h" #include "subdmesh.h" #pragma region SubDMesh #define COPY_MEMERY(len, addr)\ if (l < (len)) return false;\ ::memcpy((addr), p, (len));\ p += (len);\ l -= (len) void SubDMesh::fillPnt(const AcGePoint3d &pt, Pnt &pnt) { pnt.x = pt.x; pnt.y = pt.y; pnt.z = pt.z; } void SubDMesh::fillPt(const Pnt &pnt, AcGePoint3d &pt) { pt.x = pnt.x; pt.y = pnt.y; pt.z = pnt.z; } bool SubDMesh::subDMesh2Buffer(const AcArray &faces , const AcGePoint3dArray &pts , const AcGePoint3dArray &pts2d , void *&pOutBuffer , Adesk::UInt32 &nOutLen) { /* 内存块结构如下: face索引个数 | faces数据 | 点个数 | pts数据 | 点个数 | pts2d数据 */ Pnt pnt; std::vector vpts(0), vpts2d(0); int PntSize(0); const void *pPntStart(NULL), *pPntStart2d(NULL); if (sizeof(AcGePoint3d) != sizeof(Pnt)) { // AcGePoint3d内存在虚函数,用Pnt结构代替 PntSize = sizeof(Pnt); for (int i = 0; i < pts.length(); ++i) { const AcGePoint3d &pt(pts[i]); pnt.x = pt.x; pnt.y = pt.y; pnt.z = pt.z; vpts.push_back(pnt); } pPntStart = &vpts[0]; for (int i = 0; i < pts2d.length(); ++i) { const AcGePoint3d &pt(pts2d[i]); pnt.x = pt.x; pnt.y = pt.y; pnt.z = pt.z; vpts2d.push_back(pnt); } pPntStart2d = &vpts2d[0]; } else { // 不存在,直接用 PntSize = sizeof(AcGePoint3d); pPntStart = pts.isEmpty() ? NULL : &pts[0]; pPntStart2d = pts2d.isEmpty() ? NULL : &pts2d[0]; } int bufLen = sizeof(int) + faces.length() * sizeof(Adesk::Int32) + sizeof(int) + pts.length() * PntSize + sizeof(int) + pts2d.length() * PntSize; byte *pBuffer = new byte[bufLen]; ::memset(pBuffer, 0, bufLen); pOutBuffer = pBuffer; nOutLen = bufLen; int l(0); l = faces.length(); ::memcpy(pBuffer, &l, sizeof(int)); pBuffer += sizeof(int); if (!faces.isEmpty()) { ::memcpy(pBuffer, &faces[0], faces.length() * sizeof(Adesk::Int32)); pBuffer += faces.length() * sizeof(Adesk::Int32); } l = pts.length(); ::memcpy(pBuffer, &l, sizeof(int)); pBuffer += sizeof(int); if (!pts.isEmpty()) { ::memcpy(pBuffer, pPntStart, pts.length() * PntSize); pBuffer += pts.length() * PntSize; } l = pts2d.length(); ::memcpy(pBuffer, &l, sizeof(int)); pBuffer += sizeof(int); if (!pts2d.isEmpty()) { ::memcpy(pBuffer, pPntStart2d, pts2d.length() * PntSize); pBuffer += pts2d.length() * PntSize; } return true; } bool SubDMesh::subDMesh2Buffer(const AcDbSubDMesh & mesh, const AcGePoint3dArray & pts2d, void *& pOutBuffer, Adesk::UInt32 & nOutLen) { AcGePoint3dArray pts; AcArray faces; mesh.getVertices(pts); mesh.getFaceArray(faces); return subDMesh2Buffer(faces, pts, pts2d, pOutBuffer, nOutLen); } bool SubDMesh::Buffer2SubDMesh(void *pBuffer , const int &len , AcArray &faces , AcGePoint3dArray &pts , AcGePoint3dArray &pts2d) { faces.removeAll(); pts.removeAll(); pts2d.removeAll(); byte *p((byte*)pBuffer); int l(len); int count(0); // 先取faces长度 COPY_MEMERY(sizeof(int), &count); faces.setLogicalLength(count); // 再取faces数据 if (count) { COPY_MEMERY(count * sizeof(Adesk::Int32), &faces[0]); } // 取pts长度 COPY_MEMERY(sizeof(int), &count); if (sizeof(AcGePoint3d) != sizeof(Pnt)) { if (count) { std::vector vpts(count); COPY_MEMERY(count * sizeof(Pnt), &vpts[0]); for (size_t i = 0; i < vpts.size(); ++i) { Pnt &pt(vpts[i]); pts.append(AcGePoint3d(pt.x, pt.y, pt.z)); } } } else { if (count) { pts.setLogicalLength(count); COPY_MEMERY(count * sizeof(Pnt), &pts[0]); } } // 取pts2d长度 COPY_MEMERY(sizeof(int), &count); if (sizeof(AcGePoint3d) != sizeof(Pnt)) { if (count) { std::vector vpts(count); COPY_MEMERY(count * sizeof(Pnt), &vpts[0]); for (size_t i = 0; i < vpts.size(); ++i) { Pnt &pt(vpts[i]); pts2d.append(AcGePoint3d(pt.x, pt.y, pt.z)); } } } else { if (count) { pts2d.setLogicalLength(count); COPY_MEMERY(count * sizeof(Pnt), &pts2d[0]); } } return true; } #pragma endregion