Line drawing : Sending points from sendMessage to Autocad

Line drawing : Sending points from sendMessage to Autocad

Anonymous
Not applicable
1,539 Views
8 Replies
Message 1 of 9

Line drawing : Sending points from sendMessage to Autocad

Anonymous
Not applicable

HI All,

 

I have *.arx, which i am loading into Autocad using appload *.arx file.

I intend to draw a line by using SendCmdToAcad

 

read(string cmd )

{

wchar_t wptr[50] = {0};
cmd += "\n";
char2wchar(cmd.c_str(), wptr);
SendCmdToAcad(wptr);

}

static void SendCmdToAcad(ACHAR *cmd)
{
COPYDATASTRUCT cmdMsg;
cmdMsg.dwData = (DWORD)1;
cmdMsg.cbData = (DWORD)(_tcslen(cmd) + 1) * sizeof(ACHAR);
cmdMsg.lpData = cmd;
HWND hwnd = adsw_acadMainWnd();
SendMessage(hwnd, WM_COPYDATA, NULL, (LPARAM)&cmdMsg);
}

 

The sequence is

command: _line   -> SendMessage() works fine sends to autocad and SendMessage() returns from call

Specify first point: 10,20,0

Specify next point or [Undo]:   : After sending 1st point sendmessage blocks here , does not return from the call, until i explicitly hit "ENTER" key on Autocad window.

 

How do i solve the problem programmatically of hitting "enter" key after point.

In my read() -> above If i do 

cmd += "\n\n";

 

then it escapes reading the second point as well

Specify next point or [Undo]:

 

Please let me know, any pointers would be greatly appreciated. I need to get this done.

Thank you.


Observed Behaviour

10,20,30\0 : reads point on same line as in (10,20,30|123,300,10)
                    no | in real life just indicative of end of 1 point
10,20,30\n : blocks
10,20,30\n\n : skips the next point reading
10,20,30(1char)\n: if (1char) any character is added it goes fine, but point becomes invalid

 

0 Likes
Accepted solutions (2)
1,540 Views
8 Replies
Replies (8)
Message 2 of 9

tbrammer
Advisor
Advisor

Have you tried to use acDocManager->sendStringToExecute(doc, command)  instead?

This works fine for me:

 acDocManager->sendStringToExecute(acDocManager->curDocument(), L"_LINE 10,0,0 10,20,0 \n");

 

Note that I use one space ' ' to terminate the point and a '\n' to terminate the LINE command.

Maybe it also works for the COPYDATASTRUCT approach.


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

0 Likes
Message 3 of 9

tbrammer
Advisor
Advisor

By the way: Is there a special reason why you draw a line like this instead of creating in directly in the database?


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

0 Likes
Message 4 of 9

Anonymous
Not applicable

no reason, this was the method i saw on internet , and used it.

Added ' ' (single space) not working with COPYDATASTRUCT.

Could you please give me example of doing it by database method.

0 Likes
Message 5 of 9

tbrammer
Advisor
Advisor
Accepted solution

@Anonymous wrote:

no reason, this was the method i saw on internet , and used it.

Added ' ' (single space) not working with COPYDATASTRUCT.

Could you please give me example of doing it by database method.


I would recomment to use acDocManager->sendStringToExecute()  instead of SendMessage(hwnd, WM_COPYDATA, ..).

But you should always prefer to create objects directly in the database, because it gives you much better control and performance than sending commands! Sending "_LINE 0,0,0 100,0,0 \n" to AutoCAD doesn't guarantee that it really draws a line from 0,0,0 to 100,0,0. There might be object snap involved causing a totally different start- or endpoints to be snapped.

 

 

// Add entity ent to the modelspace of the current drawing.
Acad::ErrorStatus postToDb(AcDbEntity* ent, AcDbObjectId& objId)
{
	Acad::ErrorStatus      es;
	AcDbBlockTableRecord *pModelSpace;
	AcDbDatabase *pDB = acdbHostApplicationServices()->workingDatabase();
	AcDbObjectId IdModelSpace = acdbSymUtil()->blockModelSpaceId(pDB);

	if ((es = acdbOpenObject(pModelSpace, IdModelSpace, AcDb::kForWrite)) == Acad::eOk) 
	{
		es = pModelSpace->appendAcDbEntity(objId, ent);
		pModelSpace->close();

		if (es == Acad::eOk)
			ent->close();
	}
	return es;
}

// Create a line in the modelspace of the current drawing.
AcDbObjectId AddLine(AcGePoint3d p1, AcGePoint3d p2)
{
	AcDbObjectId objId;
	AcDbLine *line = new AcDbLine(p1, p2);
	line->setDatabaseDefaults();
	Acad::ErrorStatus es = postToDb(line, objId);
	if (es != Acad::eOk)
		delete line;
	return objId;
}

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

Message 6 of 9

Anonymous
Not applicable

Thank you tbrammer.

I also need to capture the mouse event clicked on line/symbol/text entities.

Could you please direct me to sample code of doing it.

Ive posted a question on this with title "C++: Mouse Event handling"

 

Thanks.

 

 

0 Likes
Message 7 of 9

Anonymous
Not applicable

I need to send points to Autocad through Sendmessage(), but stuck

with not able to send points to autocad, due to the listed problem in my query.

If anybody knows any workaround to send points to autocad using sendmessage, please let me know,

 

Thanks.

0 Likes
Message 8 of 9

Anonymous
Not applicable
Accepted solution

This way also it works

 

string AutoCmd="1234556.89,6823463,50,50";

wchar_t *wpt = (wchar_t *)calloc(AutoCmd.length(), sizeof(wchar_t));
char2wchar(AutoCmd.c_str(), wpt);
acedCommand(RTSTR, _T("_POINT"), RTSTR, wpt , RTNONE);
free(wpt);

 

thanks,

0 Likes
Message 9 of 9

moogalm
Autodesk Support
Autodesk Support

I recommend you to follow @tbrammer solution. 

 

 

 

Please note your code might be work, but you will be not able to scale or migrate to newer versions of AutoCAD, your application may not work post fiber world. i/e ACAD 2015.

0 Likes