Files
gjm 164968b62e chore
把非utf8-bom编码的cpp/h文件改为 utf8 bom 编码, msvc识别utf8编码时,如果不是bom格式的,会使用当前cp_oem来解码.
2026-10-04 00:04:20 +08:00

1052 lines
18 KiB
C++

#pragma once
#include <string>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <functional>
#include <algorithm>
#include "shJson.h"
using namespace SHJson;
namespace GEO2JSON
{
struct _geo
{
enum TYPE
{
// 点/向量
ePoint = 0,
// 面
ePlane,
// 矩阵
eMatrix,
// 二进制数据
eBinary,
// 直线
eLine,
// 弧
eArc,
// 圆
eCircle,
// 椭圆
eEllipse,
// 面域
eRegion,
// 拉伸体
eExtruded,
// 旋转体
eRevolved,
// 扫掠体
eSweep,
// 放样体
eLoft,
// 路径
ePath,
// 三角面
eTessellation,
// spline
eSpline,
// none
eNone,
};
template<typename T> _geo* newGeo(const WValue &v)
{
T *p = new T();
if (!p->ByJson(v)) return NULL;
return p;
}
_geo(const _geo &g);
_geo();
virtual TYPE type() = 0;
std::wstring toString();
bool byString(const std::wstring &json);
void addType(WValue &v);
TYPE getType(const WValue &v);
_geo *createGeo(const WValue &v);
void set(const std::wstring &s);
void set(const WValue &v);
virtual void set(const AcDbCurve *pCurve){}
virtual WValue& ToJson() = 0;
virtual bool ByJson(const WValue &v) = 0;
virtual _geo* clone() = 0;
virtual AcDbCurve* asDbCurve() { return NULL; }
virtual unsigned long memLen() { return 0; }
virtual byte *toBinary() { return NULL; }
virtual void byBinary(byte *p) {}
byte* makeMem()
{
unsigned long len = memLen();
byte *p = new byte[len];
::memset(p, 0, sizeof(byte) * len);
return p;
}
template <class T>
static _geo* makeGeoPtr(AcDbCurve *pCrv)
{
T *p = new T();
p->set(pCrv);
return p;
}
WValue _v;
};
// 对应 AcGePoint AcGeVector3d 对象
typedef struct point: public _geo
{
double _x, _y, _z;
point():_x(.0), _y(.0), _z(.0) {}
point(const double &x, const double &y, const double &z);
point(const AcGePoint3d &pt);
point(const AcGeVector3d &vt);
point(std::wstring &s);
point(const WValue &v);
point &operator = (const point &pt);
point &operator = (const AcGeVector3d &vt);
point &operator = (const AcGePoint3d &pt);
point &operator = (const std::wstring &s);
point &operator = (const WValue &v);
void set(const double &x, const double &y, const double &z);
void set(const AcGePoint3d &pt);
void set(const AcGeVector3d &vt);
AcGePoint3d asPnt3d();
AcGeVector3d asVec3d();
virtual WValue& ToJson();
virtual bool ByJson(const WValue &v);
virtual _geo* clone();
virtual TYPE type();
virtual unsigned long memLen();
virtual byte* toBinary();
virtual void byBinary(byte *p);
} vector;
// 对应 AcGePlane 对象
struct plane: public _geo
{
// 原点
point _origin;
// 法向
vector _normal;
plane();
plane(const point &pt, const vector &vt);
plane(const AcGePoint3d &ori, const AcGeVector3d &vt);
plane(const AcGePlane &plan);
plane(const std::wstring &s);
plane(const WValue &v);
plane &operator = (const plane &plan);
plane &operator = (const AcGePlane &plan);
plane &operator = (const std::wstring &s);
plane &operator = (const WValue &v);
void set(const AcGePoint3d &ori, const AcGeVector3d &vt);
void set(const AcGePlane &plan);
AcGePlane asGePlane();
virtual WValue& ToJson();
virtual bool ByJson(const WValue &v);
virtual _geo* clone();
virtual TYPE type();
virtual unsigned long memLen();
virtual byte* toBinary();
virtual void byBinary(byte *p);
};
// 对应 AcGeMatrix3d
struct matrix: public _geo
{
vector _x, _y, _z;
point _o;
matrix();
~matrix();
matrix(const vector &x
, const vector &y
, const vector &z
, const point &o );
matrix(const AcGeMatrix3d &m);
matrix(const matrix &m);
matrix(const std::wstring &s);
matrix(const WValue &v);
matrix &operator = (const AcGeMatrix3d &m);
matrix &operator = (std::wstring &s);
matrix &operator = (const matrix &m);
matrix &operator = (const WValue &v);
void set(const AcGeMatrix3d &m);
void set(const matrix &m);
AcGeMatrix3d asMatrix3d();
virtual WValue& ToJson();
virtual bool ByJson(const WValue &v);
virtual _geo *clone();
virtual TYPE type();
virtual unsigned long memLen();
virtual byte *toBinary();
virtual void byBinary(byte *p);
};
// 二进制数据
struct binary: public _geo
{
byte *_data;
size_t _len;
binary();
~binary();
binary(const byte *data, const size_t &len);
binary(const binary &b);
binary(const std::wstring &s);
binary(const WValue &v);
binary& operator = (const binary &b);
binary& operator = (const std::wstring &s);
binary& operator = (const WValue &v);
void set(const binary &b);
void set(const byte *data, const size_t &len);
virtual WValue& ToJson();
virtual bool ByJson(const WValue &v);
virtual _geo *clone();
virtual TYPE type();
};
// 对应 AcDbLine 对象
struct line: public _geo
{
point _start, _end;
line();
line(const point &s, const point &e);
line(const AcGePoint3d &s, const AcGePoint3d &e);
line(const AcDbLine &l);
line(const AcDbObjectId &id);
line(const std::wstring &s);
line(const WValue &v);
line &operator = (const line &l);
line &operator = (const AcDbLine &l);
line &operator = (const AcDbObjectId &id);
line &operator = (const std::wstring &s);
line &operator = (const WValue &v);
void set(const AcGePoint3d &s, const AcGePoint3d &e);
void set(const AcDbLine &l);
void set(const AcDbObjectId &id);
virtual void set(const AcDbCurve *pCurve);
AcDbLine *asDbLine();
virtual AcDbCurve *asDbCurve();
virtual WValue& ToJson();
virtual bool ByJson(const WValue &v);
virtual _geo *clone();
virtual TYPE type();
virtual unsigned long memLen();
virtual byte *toBinary();
virtual void byBinary(byte *p);
};
// 对应 AcDbArc 对象
struct arc: public _geo
{
point _start, _middle, _end;
arc();
arc(const point &s, const point &m, const point &e);
arc(const AcDbArc &a);
arc(const AcDbObjectId &id);
arc(const std::wstring &s);
arc(const WValue &v);
arc &operator = (const AcDbArc &a);
arc &operator = (const arc &a);
arc &operator = (const AcDbObjectId &id);
arc &operator = (const std::wstring &s);
arc &operator = (const WValue &v);
void set(const AcDbArc &a);
void set(const AcDbObjectId &id);
virtual void set(const AcDbCurve *pCurve);
AcDbArc *asDbArc();
virtual AcDbCurve *asDbCurve();
virtual WValue& ToJson();
virtual bool ByJson(const WValue &v);
virtual _geo *clone();
virtual TYPE type();
virtual unsigned long memLen();
virtual byte *toBinary();
virtual void byBinary(byte *p);
};
// 对应 AcDbCircle 对象
struct circle: public _geo
{
point _center;
vector _normal;
double _radius;
circle();
circle(const point &c, const vector &n, const double &r);
circle(const AcDbCircle &cir);
circle(const AcDbObjectId &id);
circle(const std::wstring &s);
circle(const WValue &v);
circle &operator = (const circle &cir);
circle &operator = (const AcDbCircle &cir);
circle &operator = (const AcDbObjectId &id);
circle &operator = (const std::wstring &s);
circle &operator = (const WValue &v);
void set(const AcDbCircle &cir);
void set(const AcDbObjectId &id);
virtual void set(const AcDbCurve *pCurve);
AcDbCircle *asDbCircle();
virtual AcDbCurve *asDbCurve();
virtual WValue& ToJson();
virtual bool ByJson(const WValue &v);
virtual _geo* clone();
virtual TYPE type();
virtual unsigned long memLen();
virtual byte *toBinary();
virtual void byBinary(byte *p);
};
// 对应 AcDbEllipse 对象
struct ellipse: public _geo
{
point _center;
vector _xAxis, _yAxis;
double _xRadius, _yRadius;
double _startParam, _endParam;
ellipse();
ellipse(const point &c
, const vector &x
, const vector &y
, const double &xr
, const double &yr
, const double &sp
, const double &ep );
ellipse(const AcDbEllipse &e);
ellipse(const AcDbObjectId &id);
ellipse(const ellipse &e);
ellipse(const std::wstring &s);
ellipse(const WValue &v);
ellipse &operator = (const ellipse &e);
ellipse &operator = (const AcDbEllipse &e);
ellipse &operator = (const AcDbObjectId &id);
ellipse &operator = (const std::wstring &s);
ellipse &operator = (const WValue &v);
void set(const ellipse &e);
void set(const AcDbEllipse &e);
void set(const AcDbObjectId &id);
virtual void set(const AcDbCurve *pCurve);
AcDbEllipse *asDbEllipse();
virtual AcDbCurve *asDbCurve();
virtual WValue& ToJson();
virtual bool ByJson(const WValue &v);
virtual _geo* clone();
virtual TYPE type();
virtual unsigned long memLen();
virtual byte *toBinary();
virtual void byBinary(byte *p);
};
// 对应 AcDbSpline 对象
struct spline: public _geo
{
int _spType;
std::vector<point> _fitPts;
Adesk::Boolean _tangentsExist;
vector _startTangent;
vector _endTangent;
int _knotParam;
int _degree_fit;
double _fitTolerance;
std::vector<point> _controlPts;
int _degree_nurb;
Adesk::Boolean _rational;
Adesk::Boolean _closed;
Adesk::Boolean _periodic;
AcGeDoubleArray _knots;
AcGeDoubleArray _weights;
double _controlPtTol;
double _knotTol;
spline();
spline(const AcDbSpline &pl);
spline(const spline &pl);
spline &operator = (const spline &pl);
spline &operator = (const AcDbSpline &pl);
spline &operator = (const AcDbObjectId &id);
void set(const AcDbSpline &cir);
void set(const AcDbObjectId &id);
void set(const spline &sp);
virtual void set(const AcDbCurve *pCurve);
virtual AcDbCurve *asDbCurve();
virtual WValue& ToJson();
virtual bool ByJson(const WValue &v);
virtual _geo* clone();
virtual TYPE type();
virtual unsigned long memLen();
virtual byte *toBinary();
virtual void byBinary(byte *p);
};
struct path: public _geo
{
typedef std::vector<_geo*> profile;
profile _profile;
plane _plane;
path();
virtual ~path();
path(const AcDbEntity &ent);
path(const AcDbObjectId &id);
path(const std::wstring &s);
path(const WValue &v);
path& operator = (const profile &pf);
path& operator = (const path &reg);
path& operator = (const AcDbEntity &ent);
path& operator = (const AcDbObjectId &id);
path& operator = (const std::wstring &s);
path& operator = (const WValue &v);
void set(const path &pt);
void set(const path::profile &pf);
void set(const AcDbEntity &ent);
void set(const AcDbObjectId &id);
virtual void set(const AcDbCurve *pCurve);
void clear();
bool isEmpty();
virtual AcDbCurve *asDbCurve();
AcDbRegion* asDbRegion();
virtual WValue &ToJson();
virtual bool ByJson(const WValue &v);
virtual _geo *clone();
virtual TYPE type();
virtual byte *toBinary();
virtual void byBinary(byte *p);
virtual unsigned long memLen();
};
// 对应 AcDbRegion AcDbPolyline 对象
struct region: public _geo
{
typedef std::vector<path*> profiles;
profiles _profiles;
plane _plane;
bool _b2D;
region();
virtual ~region();
region(const AcDbEntity &ent, bool b2D = false);
region(const AcDbObjectId &id, bool b2D = false);
region(const std::wstring &s, bool b2D = false);
region(const WValue &v, bool b2D = false);
region& operator = (const region &reg);
region& operator = (const AcDbEntity &ent);
region& operator = (const AcDbObjectId &id);
region& operator = (const std::wstring &s);
region& operator = (const WValue &v);
void set(const region &reg);
void set(const AcDbEntity &ent);
void set(const AcDbObjectId &id);
virtual AcDbCurve *asDbCurve();
void asDbCurve(std::vector<AcDbCurve*> &crvs);
void asDbRegion(std::vector<AcDbRegion*> &regs);
AcDbRegion* asDbRegion();
void clear();
bool isEmpty();
virtual WValue& ToJson();
virtual bool ByJson(const WValue &v);
virtual _geo *clone();
virtual TYPE type();
};
// 对应 拉伸体
struct extruded: public _geo
{
region _region;
path _path;
extruded();
extruded(const region &r, const path &p);
extruded(const AcDbEntity &reg, const AcDbCurve &curve);
extruded(const AcDbObjectId &reg, const AcDbObjectId &curve);
extruded(const std::wstring &s);
extruded(const WValue &v);
virtual ~extruded();
extruded &operator = (const extruded &ed);
extruded &operator = (const std::wstring &s);
extruded &operator = (const WValue &v);
void set(const AcDbEntity &reg, const AcDbCurve &curve);
void set(const region &r, const path &p);
void set(const AcDbObjectId &reg, const AcDbObjectId &curve);
AcDbCurve* DbRegion1();
AcDbRegion* DbRegion2();
AcDbCurve* DbPath();
virtual WValue& ToJson();
virtual bool ByJson(const WValue &v);
virtual _geo* clone();
virtual TYPE type();
};
// 对应 旋转体
struct revolved: public extruded
{
revolved();
revolved(const AcDbEntity &reg, const AcDbCurve &curve);
revolved(const AcDbObjectId &reg, const AcDbObjectId &curve);
revolved(const std::wstring &s);
revolved(const WValue &v);
revolved &operator = (const revolved &ed);
revolved &operator = (const std::wstring &s);
revolved &operator = (const WValue &v);
virtual TYPE type();
};
// 对应 扫掠体
struct sweep: public extruded
{
sweep();
sweep(const AcDbEntity &reg, const AcDbCurve &curve);
sweep(const AcDbObjectId &reg, const AcDbObjectId &curve);
sweep(const std::wstring &s);
sweep(const WValue &v);
sweep &operator = (const sweep &ed);
sweep &operator = (const std::wstring &s);
sweep &operator = (const WValue &v);
virtual TYPE type();
};
// 对应 放样体
struct loft: public _geo
{
std::vector<region*> _regions;
loft();
loft(std::vector<region*> regs);
loft(const AcDbVoidPtrArray &regs);
loft(const AcDbObjectIdArray &ids);
loft(const std::wstring &s);
loft(const WValue &v);
virtual ~loft();
void getRegions(AcDbVoidPtrArray &ar, const bool &byPoly);
void set(const AcDbVoidPtrArray &regs);
void set(const AcDbObjectIdArray &ids);
void set(const std::vector<region*> &regs);
loft &operator = (const loft &l);
loft &operator = (const std::wstring &s);
loft &operator = (const WValue &v);
virtual WValue& ToJson();
virtual bool ByJson(const WValue &v);
virtual _geo* clone();
virtual TYPE type();
};
struct tessellation: public _geo
{
AcGePoint3dArray _pts;
int _r, _g, _b;
tessellation(): _r(0)
, _g(0)
, _b(0){}
tessellation(const AcGePoint3dArray &pts, const DWORD dwClr) { set(pts, dwClr); }
void set(const AcGePoint3dArray &pts, const DWORD dwClr);
tessellation &operator = (const tessellation &t);
virtual WValue& ToJson();
virtual _geo* clone();
virtual ~tessellation() {}
virtual bool ByJson(const WValue &v) { return false; }
virtual TYPE type() { return eTessellation; }
};
typedef std::vector<tessellation> tessellations;
class CMemTools
{
public:
CMemTools(byte *ptr): m_pCurPtr(ptr), m_pOrignPtr(ptr){}
void read(byte *pData, const unsigned long &len)
{
::memcpy(pData, m_pCurPtr, len);
m_pCurPtr += len;
}
template <typename T>
void read(T *pData)
{
int len(sizeof(T));
::memcpy(pData, m_pCurPtr, len);
m_pCurPtr += len;
}
template <typename T>
void read(T *pData, const int &num)
{
int len(sizeof(T) * num);
::memcpy(pData, m_pCurPtr, len);
m_pCurPtr += len;
}
CString read_string()
{
CString str(m_pCurPtr);
m_pCurPtr += (str.GetLength() + 1) * sizeof(TCHAR);
}
point read_point()
{
point pt;
read<double>(&pt._x);
read<double>(&pt._y);
read<double>(&pt._z);
return pt;
}
void write(const byte *pData, const unsigned long &len, bool bErase = false)
{
if (!pData) return;
::memcpy(m_pCurPtr, pData, len);
m_pCurPtr += len;
if (bErase)
delete[] pData;
}
template <typename T>
void write(const T *pData)
{
if (!pData) return;
int len(sizeof(T));
::memcpy(m_pCurPtr, pData, len);
m_pCurPtr += len;
}
template <typename T>
void write(const T *pData, const int &num)
{
if (!pData) return;
int len(sizeof(T) * num);
::memcpy(m_pCurPtr, pData, len);
m_pCurPtr += len;
}
void write_string(LPCTSTR psz)
{
if (!psz) return;
CString s(psz);
int len = sizeof(TCHAR) * (s.GetLength() + 1);
::memcpy(m_pCurPtr, psz, len);
m_pCurPtr += len;
}
void write_point(const point *pt)
{
write<double>(&pt->_x);
write<double>(&pt->_y);
write<double>(&pt->_z);
}
void seekTo(byte *p) { m_pCurPtr = p; }
void seekToBegin() { seekTo(orignPos()); }
void forward(unsigned long len) { m_pCurPtr += len; }
byte* curPos() { return m_pCurPtr; }
byte* orignPos() { return m_pOrignPtr; }
private:
byte *m_pCurPtr;
byte *m_pOrignPtr;
};
struct tools
{
static void asCurves(const std::vector<_geo *> &geos, AcDbVoidPtrArray &ar);
static void fromEntity(region::profiles &pfs, AcGePlane &plan, const AcDbEntity &ent, const bool &b2D);
static void fromEntity(std::vector<_geo *> &geos, AcGePlane &plan, const AcDbEntity &ent);
static AcDbCurve *asDbPolyline(const std::vector<_geo *> &geos);
static AcDbRegion *asDbRegion(const std::vector<_geo *> &geos);
static AcGePlane getPlane(const AcDbEntity &ent);
// 为导出到revit,处理过短线
static void MeragePolyline(AcDbPolyline *pPoly, const double &tol);
static bool fromFace(tessellations &ss, AcDbFace *pFace);
// 根据精度来创建loop,两点距离小于tol则合并为一个点
static int makeVertexLoop(const AcGePoint3dArray &pts, AcGePoint3dArray &loop, const double &tol = 2);
static int solid2Face(AcDb3dSolid *pSolid, tessellations &ss);
static _geo* makeGeoPtr(byte *p)
{
using namespace GEO2JSON;
int type;
CMemTools mem(p);
mem.read<int>(&type);
_geo *pGeo = NULL;
switch (type)
{
case _geo::ePoint:
pGeo = new point;
break;
case _geo::ePlane:
pGeo = new plane;
break;
case _geo::eMatrix:
pGeo = new matrix;
break;
case _geo::eLine:
pGeo = new line;
break;
case _geo::eArc:
pGeo = new arc;
break;
case _geo::eCircle:
pGeo = new circle;
break;
case _geo::eEllipse:
pGeo = new ellipse;
break;
case _geo::eRegion:
pGeo = new path;
break;
case _geo::ePath:
pGeo = new path;
break;
case _geo::eSpline:
pGeo = new spline;
break;
default:
break;
}
if (pGeo)
{
pGeo->byBinary(p);
}
return pGeo;
}
};
}