What's wrong?

What's wrong?

463017170
Advocate Advocate
784 Views
4 Replies
Message 1 of 5

What's wrong?

463017170
Advocate
Advocate

 

 

 

 

 

static void MSDDMyGroupMyCommand0 () {
	ads_name ss;
	resbuf *rbList = acutBuildList(RTDXF0, _T("LWPOLYLINE"),RTNONE);
	if (RTNORM != acedSSGet(NULL, NULL, NULL, rbList, ss))
	{
		acutRelRb(rbList);
		return;
	}
	acutRelRb(rbList);

	Adesk::Int32 nSSLength = 0;
	acedSSLength(ss, &nSSLength);
	vector<AcGePoint2dArray> Ptss;
	AcGePoint2dArray ptstemp;
	for (Adesk::Int32 i = 0; i < nSSLength; i++)
	{
		ads_name ent;
		acedSSName(ss, i, ent);
		AcDbObjectId objId;
		acdbGetObjectId(objId, ent);
		GetAcDb2dPolyPoints(objId,ptstemp);
		Ptss.push_back(ptstemp);
	}
	vector<vector<AcGePoint2dArray>> ptsu,ptsn;
	GetConnected(Ptss, ptsu,ptsn);
	acedSSFree(ss);

	acutPrintf(_T("\n Enclosed area: %d "),ptsu.size());
	acutPrintf(_T("\nOpen area: %d "),ptsn.size());
}


static void GetConnected(vector<AcGePoint2dArray> Ptss, vector<vector<AcGePoint2dArray>> &VPtss, vector<vector<AcGePoint2dArray>> &VtempC)
{
	while (Ptss.size() > 0)
	{
		vector<AcGePoint2dArray> tempY,tempN,tempC;

		AcGePoint2d sta = Ptss.back().first();   
		AcGePoint2d end = Ptss.back().last();
		tempY.push_back(Ptss.back());
		Ptss.pop_back();
		tempN.clear();

		bool TN=true;
		while ((sta.distanceTo(end) > 1e-7) && TN)
		{
			int stalen = (int)Ptss.size();
			for (Adesk::Int32 i = 0; i < Ptss.size(); i++)
			{
				AcGePoint2d sta1 = Ptss.at(i).first();   
				AcGePoint2d end1 = Ptss.at(i).last();
				double kk1 = sta.distanceTo(sta1);
				double kk2 = sta.distanceTo(end1);

				if ( kk1 <= 1e-7 || kk2 <= 1e-7 )
				{
					if  ( bool kk = (kk1 <= 1e-7))
					{
						Ptss.at(i).reverse();	
						sta = Ptss.at(i).at(0);
						tempY.push_back(Ptss.at(i));
                                                Ptss.removeat(i);
						 
					}
					if ( bool kk = (kk2 <= 1e-7))
					{
						sta = Ptss.at(i).at(0);
						tempY.push_back(Ptss.at(i));
						  Ptss.removeat(i);
					}
				} 
				else
				{
					tempN.push_back(Ptss.at(i));
				}

			} //for end
			int entlen = (int)Ptss.size();

	 
		}//(sta.distanceTo(end)!= 1e-7) end
		VPtss.push_back(tempY);
		Ptss=tempN;
	}//while(Ptss.size() > 0) end
}

	static void  GetAcDb2dPolyPoints(AcDbObjectId id, AcGePoint2dArray &ptas)
{
	AcDbEntity *pEnt = NULL;
	acdbOpenObject(pEnt, id, AcDb::kForWrite);
	if (pEnt->isKindOf(AcDbPolyline::desc()))
	{
		AcDbPolyline *pPline=AcDbPolyline::cast(pEnt);
		int num =pPline->numVerts();
		AcGePoint2d pt;
		for (int i=0;i<num;i++)
		{		
			pPline->getPointAt(i,pt);
			ptas.append(pt);
		}
		pPline->close();
	}
}

 

 

 

 

 

0 Likes
Accepted solutions (1)
785 Views
4 Replies
Replies (4)
Message 2 of 5

tbrammer
Advisor
Advisor

I can't compile your code because of:

 

 

static void GetConnected(vector<AcGePoint2dArray> Ptss,...)
   Ptss.removeat(i);  // std::removeat() doesn't exist
   // Ptss.erase(Ptss.begin() + i); // possible solution with std::vector<>

 

 

I suppose that vector<> is meant to be std::vector<>.

If I fix the code as mentioned in the comment above I run into an infinite loop in GetConnected(..)

because the loop   while ((sta.distanceTo(end) > 1e-7) && TN)   is never left if there is no matching curve.

 

I think there is no reason to retrieve all polyline points. You only need start-and endpoint if the curve isn't closed.

Use bool AcDbCurve::isClosed() and if the curve isn't close get the start/endpoint with

AcDbCurve::getStartPoint(pStart) and  AcDbCurve::getEndPoint(pEnd).

 

I would recommend not to use the 2D-points from pPline->getPointAt(i, pt). These 2D points refer to the ECS of the polyline. If you happen to find polylines with different ECS it makes no sense to compare the 2D points.

 


Thomas Brammer ● Software Developer ● imos AGLinkedIn
If an answer solves your problem please [ACCEPT SOLUTION]. Otherwise explain why not.

0 Likes
Message 3 of 5

463017170
Advocate
Advocate

Thank you for your help. I've changed it. I just don't know if there is a faster way?

0 Likes
Message 4 of 5

tbrammer
Advisor
Advisor
Accepted solution

Have a look at the code in curveChain.cpp in the attached ZIP.
I think it is effective and understandable.


Thomas Brammer ● Software Developer ● imos AGLinkedIn
If an answer solves your problem please [ACCEPT SOLUTION]. Otherwise explain why not.

0 Likes
Message 5 of 5

463017170
Advocate
Advocate

thank you!

0 Likes