You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kev/Drawer/Module/GeoSigmaDraw/InterfaceGeometry.cpp

74 lines
1.9 KiB
C++

#include "StdAfx.h"
#include "keyholing.h"
extern "C" __declspec(dllexport)
BSTR ConnectPolygons(LPCTSTR blockLines) {
//Clipper2Lib::ConvexHull()
CString strBlock(blockLines);
// 将 "\r\n" 替换为 "\n"
strBlock.Replace(_T("\r\n"), _T("\n"));
vector<CString> vecString;
CString strTmp;
int iPos = 0;
while (AfxExtractSubString(strTmp, blockLines, iPos, '\n'))
{
iPos++;
vecString.push_back(strTmp);
}
Paths64 subjects;
double dX, dY;
Path64* pPathtmp = nullptr;
for (auto iter = vecString.begin(); iter != vecString.end(); ++iter) {
CString strLine = *iter;
if (strLine.Find("Pline") == 0) {
if (pPathtmp != nullptr) {
subjects.push_back(*pPathtmp);
delete pPathtmp;
}
pPathtmp = new Path64;
continue;
}
int nCount = sscanf(strLine, "%lf,%lf", &dX, &dY);
if (nCount >= 2) {
Point64 pt64 = toPoint64(dX, dY); //, pCurve->z[i]
pPathtmp->emplace_back(pt64.x, pt64.y);
}
}
if (subjects.size() == 0) {
return 0;
}
PolyTree64 polytree;
Clipper64 c64;
c64.AddSubject(subjects);
c64.Execute(ClipType::Union, FillRule::EvenOdd, polytree);
// 首先合并相交或包含的多边形
Paths64 solutionMerge;
if (!KeyHole(polytree, solutionMerge)) return false;
Path64 pathConn64 = ConnectPolygons(solutionMerge);
Path64 pathResult64 = TrimCollinear(pathConn64);
int nSize = pathResult64.size();
if (nSize == 0) {
return nullptr;
}
if (pathResult64.at(0).x != pathResult64.at(nSize-1).x && pathResult64.at(0).y != pathResult64.at(nSize-1).y)
{
pathResult64.emplace_back(Point64(pathResult64[0].x, pathResult64[0].y));
}
PathD pathResult = fromPolygon64(pathResult64);
CString strData;
CString strX, strY;
for (const auto& item : pathResult)
{
AfxGetPublicFunction()->FloatToString(strX, item.x, 6);
AfxGetPublicFunction()->FloatToString(strY, item.y, 6);
strData += ( strX + "," + strY + "\r\n");
}
return strData.AllocSysString();
}