Author Topic: Unsafe code and debug  (Read 10746 times)

0 Members and 1 Guest are viewing this topic.

exmachina

  • Guest
Re: Unsafe code and debug
« Reply #15 on: March 08, 2012, 08:31:54 PM »
Ok. Look at 'public static class CommandLine'. This works well (at least in acad 2007) in the IDE, in 'debug' and also in 'release';  and without the IDE

Is based on an example provided by Tony Tanzillo.

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Unsafe code and debug
« Reply #16 on: March 08, 2012, 09:12:21 PM »
Thanks but I don't think that is any different than what I am using (It's Tony's as well).
I can't even figure out why it works sometimes, especially when I add a messagebox, but changing anything in the flatshot gui  nearly always causes a crash.

exmachina

  • Guest
Re: Unsafe code and debug
« Reply #17 on: March 08, 2012, 09:39:10 PM »
Sorry. I am not a programmer and I'm not a regular user of Autocad.
Perhaps the failure is due to a limitation of acedcmd (or flatshot command) or maybe not using the correct arguments. I searched through the documentation of ObjectARX, but I have not found any explanation on this problem

I tried your code but I can not reproduce the problem. The code ask for a point. Then enters in a loop which calls the 'flatshot' command , but nothing is crashed???
« Last Edit: March 08, 2012, 10:08:36 PM by exmachina »

TheMaster

  • Guest
Re: Unsafe code and debug
« Reply #18 on: March 09, 2012, 03:27:43 AM »
(IntPtr)args.UnmanagedObject.ToPointer()  AND  args.UnmanagedObject  print out as system.intPtr so I am lost on this one.

They may both print out as IntPtr because *void has no managed representation.

The ToPointer() method returns the raw pointer value that can
only be used in unsafe code, but there's no need for it.

See http://msdn.microsoft.com/en-us/library/system.intptr.topointer.aspx

I think that may have been one of the first posts I made about
P/Invoke and acedCmd(), so it was probably wrong. There's no
need for unsafe code, you just do this:

      acedCmd( resbuf.UnmanagedObject );


kaefer

  • Guest
Re: Unsafe code and debug
« Reply #19 on: March 09, 2012, 04:30:57 AM »
Perhaps the failure is due to a limitation of acedcmd (or flatshot command)

That's the most plausible explanation.

Did anyone try late bound SendCommand (demonstrated here) on this one?

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Unsafe code and debug
« Reply #20 on: March 09, 2012, 05:38:22 PM »
Kaeker, I tried it and it is not asynchronous.(If I got the right word) and the 3 commands execute after the code.

exmachina, thanks for trying but the code works fine up till acad 2008,  crashes one time only per computer in 2009 to 2011 then works fine. 2012  hardly ever. So I think it is a net version issue.

TheMaster- I tried    acedCmd( resbuf.UnmanagedObject ); but no difference.
I think it is the fact that the command has a gui that causes the problem.
I'm just experimenting w/ filedia=0;


TheMaster

  • Guest
Re: Unsafe code and debug
« Reply #21 on: March 10, 2012, 01:36:58 PM »
Kaeker, I tried it and it is not asynchronous.(If I got the right word) and the 3 commands execute after the code.

exmachina, thanks for trying but the code works fine up till acad 2008,  crashes one time only per computer in 2009 to 2011 then works fine. 2012  hardly ever. So I think it is a net version issue.

TheMaster- I tried    acedCmd( resbuf.UnmanagedObject ); but no difference.
I think it is the fact that the command has a gui that causes the problem.
I'm just experimenting w/ filedia=0;

I've seen bad things happen when acedCmd() is used inside a transaction boundary.


kaefer

  • Guest
Re: Unsafe code and debug
« Reply #22 on: March 11, 2012, 12:01:04 PM »
Kaeker, I tried it and it is not asynchronous.(If I got the right word) and the 3 commands execute after the code.

Disagree: the exection of your code is stayed while SendCommand waits for completion of the dialog (it is synchronous, just what Kean said). The problem at least with my AutoCAD is that there's no command line version of the FLATSHOT command.

TheMaster

  • Guest
Re: Unsafe code and debug
« Reply #23 on: March 11, 2012, 12:15:50 PM »
Kaeker, I tried it and it is not asynchronous.(If I got the right word) and the 3 commands execute after the code.

Disagree: the exection of your code is stayed while SendCommand waits for completion of the dialog (it is synchronous, just what Kean said). The problem at least with my AutoCAD is that there's no command line version of the FLATSHOT command.

If I'm not mistaken, if a dialog temporarily hides itself to allow the user to supply graphical input (e.g, picking points), SendCommand() can not run synchronously. 


kaefer

  • Guest
Re: Unsafe code and debug
« Reply #24 on: March 11, 2012, 01:02:12 PM »
If I'm not mistaken, if a dialog temporarily hides itself to allow the user to supply graphical input (e.g, picking points), SendCommand() can not run synchronously.

Can acedCmd, apart from an indication that something went wrong?

Take this command string:
Code: [Select]
string args = System.String.Format("_Flatshot {0},{1},{2} 1.0 1.0 0.0\n", p.X, p.Y, p.Z);

Picking an existing block won't work. But feeding it to SendCommand and just clicking "Create" kind of does, in a way, perhaps.
« Last Edit: March 11, 2012, 01:10:03 PM by kaefer »

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Unsafe code and debug
« Reply #25 on: March 11, 2012, 05:06:56 PM »
well acedCmd worked fine in 2011. Part of the function requires changing the layer in the gui to  ByLayer.  So in 2011 express 2008  it worked.
I tried not having it in a transaction and no diference.
CommandFlags.Interruptible doesn't help
No problem when using CommandFlags.NoMultiple or was it Singlethread (Sorry I have express 2008 at home and not available) as the gui never comes up

kaefer  apologies for spelling your name wrong in previous post, was typo

TheMaster

  • Guest
Re: Unsafe code and debug
« Reply #26 on: March 11, 2012, 06:22:13 PM »
If I'm not mistaken, if a dialog temporarily hides itself to allow the user to supply graphical input (e.g, picking points), SendCommand() can not run synchronously.

Can acedCmd, apart from an indication that something went wrong?

Take this command string:
Code: [Select]
string args = System.String.Format("_Flatshot {0},{1},{2} 1.0 1.0 0.0\n", p.X, p.Y, p.Z);

Picking an existing block won't work. But feeding it to SendCommand and just clicking "Create" kind of does, in a way, perhaps.

It actually has a lot to do with how Flatshot is implemented. When using
acedCmd() from an ObjectARX command, there is a risk of running out of
command-nesting levels (which I believe is 4 levels). So, if your command
executes an AutoCAD command that itself executes one or more other
AutoCAD commands internally, the number of nested command invocations
can be more than AutoCAD allows, and the whole thing goes up on smoke.

The way around that is to use [LispFunction("C:COMMANDNAME")] rather
than [CommandMethod("COMMANDNAME")], to define the command.

Many internal commands check to see if they're being scripted from LISP
(or acedCmd() more precisely) and do things differently to accommodate
that, so I suspect that because it is very much by-design that FLATSHOT
not be scriptable, it most-likely doesn't do much in support of scripting, and
could even be doing things to sabotage attempts to script it.

Another cause of many problems with scripting AutoCAD commands that
have GUIs, is the Action Recorder (The layer dialogs are an example). To
be recordable a GUI command does everything by sending commands to
the command line using an internal equivalent of SendStringToExecute().
That can really mess up attempts to script commands too.

Other possible causes of problems are running acedCmd() inside of a
transaction, and not ensuring that a command that is started by a call
to acedCmd() has ended before the command that calls it ends, which
usually crashes AutoCAD because nested transactions are not ended
in the reverse order in which they were started.

« Last Edit: March 11, 2012, 06:31:29 PM by TheMaster »

kaefer

  • Guest
Re: Unsafe code and debug
« Reply #27 on: March 11, 2012, 06:49:14 PM »
kaefer  apologies for spelling your name wrong in previous post, was typo

Bryco, apologies too for believing your original code was beyond redemption.

If I change the extern declaration and wrapper to that what TheMaster said, viz.
Code - C#: [Select]
  1.         [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
  2.             EntryPoint = "acedCmd")]
  3.         extern static ErrorStatus acedCmd(IntPtr pResbuf);
  4.  
  5.         static ErrorStatus Command(ResultBuffer args)
  6.         {
  7.             if (!acadApp.DocumentManager.IsApplicationContext)
  8.                 return acedCmd(args.UnmanagedObject);
  9.             else
  10.                 return ErrorStatus.InvalidContext;
  11.         }

it starts to make sense. It is still very ugly (mixing of manipulation of system variables, managed Editor properies inside transactions and command invokations just doesn't feel right - especially not commands that weren't designed with scripting in mind).

The SendCommand() variant runs (or not, depending on user input) with CommandFlags.Session and copious amounts of Document locking, to make AcadApplication aware of the changes to its state. An exhibit is attached.


« Last Edit: March 11, 2012, 08:56:30 PM by kaefer »

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Unsafe code and debug
« Reply #28 on: March 12, 2012, 02:28:29 PM »
kaefer that seems to work just fine, thanks and well done. I guess the command couldn't be  synchronous without CommandFlags.Session. I tried everything but that thinking it was about working in different drawings not just different threads