#include "StdAfx.h" #include "globalfunc.h" #include "GePoly3D.h" #include "AcDbObjectPtr.h" GePoly3D::GePoly3D(int nFlag) :GePoly2D(nFlag) { } GePoly3D::GePoly3D(const GePoly2D& poly) :GePoly2D(poly) { } GePoly3D::GePoly3D(const EntitySet & e) : GePoly2D(e) { EntitySet entPLine = e; AcGeVector3d vt(0,0,1); if (entPLine.Is(_T("LWPOLYLINE"))) { entPLine.GetData(210, &vt); } else if (entPLine.Is(_T("CIRCLE"))) { AcDbObjectPtr obj(entPLine,AcDb::kForRead); AcDbCircle *pCircle = (AcDbCircle *)obj.m_pObject; vt = pCircle->normal(); } else if (entPLine.Is(_T("ARC"))) { AcDbObjectPtr obj(entPLine,AcDb::kForRead); AcDbArc *pArc = (AcDbArc*) obj.m_pObject; vt = pArc->normal(); } else { return; } *this = PlaneToWcs(vt);// wulw; // AcGeMatrix3d mat; // mat.setToPlaneToWorld(vt); // TransformBy(mat); } GePoly3D::~GePoly3D() { } bool GePoly3D::IsPlanear() const { if (Length() < 2) { return true; } AcGePlane plane((*this)[0].AsAcGePoint3d(),GetNormal(false)); for (long i = 1; i < Length(); i++) { if (!plane.isOn((*this)[i].AsAcGePoint3d())) { return false; } } return true; } AcGeVector3d GePoly3D::GetNormal(bool bCheckConcave)const { AcGeVector3d vt0,vt1; bool bFindVt0 = false,bFindVt1 = false; for (long i = 0; i < Length(); i++) { AcGeVector3d vt = ((*this)[(i+1) % Length()] - (*this)[i]).AsAcGeVector3d(); if (!vt.isZeroLength()) { if (bFindVt0) { if (!vt.isParallelTo(vt0)) { vt1 = vt; bFindVt1 = true; break; } } else { vt0 = vt; bFindVt0 = true; } } } AcGeVector3d vtRes; if (bFindVt0 && bFindVt1) { vtRes = vt0.crossProduct(vt1).normal(); } else if (bFindVt0 && !vt0.isPerpendicularTo(AcGeVector3d::kZAxis)) { vtRes = vt0.perpVector().normal(); } else { vtRes = AcGeVector3d::kZAxis; } if (bCheckConcave && TotalSegments() > 3) { AcGeMatrix3d mat; mat.setToWorldToPlane(vtRes); GePoly3D temp = (*this); temp.TransformBy(mat); temp.SetElevation(0); if (temp.PathArea() > 0) { vtRes *= -1; } } return vtRes; } GePoly2D GePoly3D::WcsToPlane() const { AcGeVector3d vtNormal = GetNormal(true); AcGeMatrix3d mat; mat.setToWorldToPlane(vtNormal); GePoly2D polyTemp = *this; polyTemp.TransformBy(mat); return polyTemp; } GePoly2D GePoly3D::WcsToPlane(const AcGeVector3d& vtNormal) const { AcGeMatrix3d mat; mat.setToWorldToPlane(vtNormal); GePoly2D polyTemp = *this; polyTemp.TransformBy(mat); return polyTemp; } AcDbCurve* GePoly3D::AsAcDbCurve() const { if (IsPlanear()) { AcGeVector3d vtNormal = GetNormal(true); AcGeMatrix3d mat; mat.setToWorldToPlane(vtNormal); GePoly2D polyTemp = *this; polyTemp.TransformBy(mat); AcDbCurve* pCurve = polyTemp.AsAcDbCurve(); if (pCurve != NULL) { AcDbPolyline* pPline = (AcDbPolyline*)pCurve; pPline->setNormal(vtNormal); } return pCurve; } else { AcDb3dPolyline* pPLine = new AcDb3dPolyline; AcGePoint3d pt; for (long i = 0; i < Length(); i++) { pPLine->appendVertex(new AcDb3dPolylineVertex((*this)[i].AsAcGePoint3d())); } if(IsClosed()) { pPLine->makeClosed(); } else { pPLine->makeOpen(); } return pPLine; } return NULL; } bool GePoly3D::SetAcDbCurve(AcDbCurve*pCurve) { bool bRet = false; if (pCurve->isKindOf(AcDbPolyline::desc())) { AcDbPolyline*pPLine = (AcDbPolyline*)pCurve; if (GePoly2D::SetAcDbCurve(pPLine)) { AcGeMatrix3d mat; mat.setToPlaneToWorld(pPLine->normal()); TransformBy(mat); } } else if (pCurve->isKindOf(AcDb3dPolyline::desc())) { AcDb3dPolyline* pPLine = (AcDb3dPolyline*)pCurve->clone(); AcDbObjectId id = GlobalFunc::AddToModelSpace(pPLine,Adesk::kFalse); if (!id.isNull()) { AcDbObjectIterator* pIter = pPLine->vertexIterator(); if (pIter != NULL) { Reset(); for (pIter->start(); !pIter->done(); pIter->step()) { AcDbObjectId vertId = pIter->objectId(); AcDb3dPolylineVertex*pVertex = NULL; acdbOpenObject(pVertex, vertId, AcDb::kForRead); if (pVertex != NULL) { AppendNode(pVertex->position()); pVertex->close(); } } delete pIter; bRet = true; } if (pPLine->isClosed()) { SetFlag(1); } else { SetFlag(0); } pPLine->erase(); pPLine->close(); } else { delete pPLine; } } return bRet; } // GePoly3D::GePoly3D(int nFlag) // { // if (m_pEntityImp != NULL) // { // delete m_pEntityImp; // m_pEntityImp = NULL; // } // m_pEntityImp = new GePoly3D((TADSGePointList*)this, nFlag); // } // // GePoly3D::GePoly3D(const GePoly2D& poly) // { // if (m_pEntityImp != NULL) // { // delete m_pEntityImp; // m_pEntityImp = NULL; // } // //GePoly3D& ppoly3d = (GePoly3D& )poly; // GePoly3D* pImp = (GePoly3D*)poly.GetEntityImp(); // if (pImp != NULL) // { // m_pEntityImp = new GePoly3D((TADSGePointList*)this, *pImp); // } // } // // GePoly3D::GePoly3D(const EntitySet & e) // { // if (m_pEntityImp != NULL) // { // delete m_pEntityImp; // m_pEntityImp = NULL; // } // m_pEntityImp = new GePoly3D((TADSGePointList*)this, e); // // } // // GePoly3D::~GePoly3D() // { // // } // // bool GePoly3D::IsPlanear()const // { // ASSERT(m_pEntityImp != NULL); // return ((GePoly3D*)m_pEntityImp)->IsPlanear(this); // // } // // AcGeVector3d GePoly3D::GetNormal(bool bCheckConcave)const // { // ASSERT(m_pEntityImp != NULL); // return ((GePoly3D*)m_pEntityImp)->GetNormal(this, bCheckConcave); // } // // GePoly2D GePoly3D::WcsToPlane()const // { // ASSERT(m_pEntityImp != NULL); // GePoly3D poly3d = *this; // ((GePoly3D*)poly3d.m_pEntityImp)->WcsToPlane(this); // return poly3d; // } // // GePoly2D GePoly3D::WcsToPlane(const AcGeVector3d& vtNormal)const // { // ASSERT(m_pEntityImp != NULL); // GePoly3D poly3d = *this; // ((GePoly3D*)poly3d.m_pEntityImp)->WcsToPlane(vtNormal); // return poly3d; // } // // AcDbCurve* GePoly3D::AsAcDbCurve()const // { // ASSERT(m_pEntityImp != NULL); // return ((GePoly3D*)m_pEntityImp)->AsAcDbCurve(this); // } // // bool GePoly3D::SetAcDbCurve(AcDbCurve*pCurve) // { // ASSERT(m_pEntityImp != NULL); // return ((GePoly3D*)m_pEntityImp)->SetAcDbCurve(pCurve); // } bool GePoly3D::IsKindOf(GeEntity::TEntityId entType) const { switch(entType) { case GeEntity::eEntity: case GeEntity::eCurve: case GeEntity::ePoly2D: case GeEntity::ePoly3D: return true; } return false; } GeEntity::TEntityId GePoly3D::Type() const { return GeEntity::ePoly3D; }