Author Topic: Use editor.Command to change an objects space.  (Read 3205 times)

0 Members and 1 Guest are viewing this topic.

Keith Brown

  • Swamp Rat
  • Posts: 601
Use editor.Command to change an objects space.
« on: August 19, 2016, 09:38:31 AM »
I am trying to move some objects from modelspace into paperspace.  I get an eInvalidInput when I use the editor.Command to automate the CHSPACE command.  It does not matter what I put in the command it will always error out.  I am not sure what I am doing wrong.


Here is the code snippet that I am working with.  I am already in PaperSpace when the code is ran.


Code - C#: [Select]
  1. public class DimensionAdderStrategy : IDimensionAdderStrategy
  2. {
  3.         public void AddDimensions(List<DimensionLine> dimensionLines)
  4.         {
  5.                 using (Transaction tr = Active.Database.TransactionManager.StartTransaction())
  6.                 {
  7.                         BlockTableRecord ms = Active.Database.ModelSpace(OpenMode.ForWrite);
  8.                         ObjectId[] dimensions = new ObjectId[dimensionLines.Count];
  9.                         int index = 0;
  10.                         foreach (DimensionLine dimensionLine in dimensionLines)
  11.                         {
  12.                                 RotatedDimension rotatedDimension = new RotatedDimension();
  13.                                 double angle = AngleFromXAxis(dimensionLine.Start, dimensionLine.End);
  14.                                 rotatedDimension.XLine1Point = dimensionLine.Start;
  15.                                 rotatedDimension.XLine2Point = dimensionLine.End;
  16.                                 rotatedDimension.Rotation = angle;
  17.                                 rotatedDimension.DimLinePoint = PolarPoint(dimensionLine.End, angle + Math.PI / 2, dimensionLine.Offset);
  18.                                 rotatedDimension.DimensionStyle = Active.Database.DimStyleTableId;
  19.                                 dimensions[index] = ms.AppendEntity(rotatedDimension);
  20.                                 tr.AddNewlyCreatedDBObject(rotatedDimension, true);
  21.                                 index++;
  22.                         }
  23.  
  24.                         tr.Commit();
  25.                         SelectionSet selectionSet = SelectionSet.FromObjectIds(dimensions);
  26.                         Active.Editor.Command("MSPACE", "", selectionSet, "", "CHSPACE", "");
  27.                 }
  28.         }
  29.  
  30.         private double AngleFromXAxis(Point3d start, Point3d end)
  31.         {
  32.                 return new Vector2d(end.X - start.X, end.Y - start.Y).Angle;
  33.         }
  34.  
  35.         public Point3d PolarPoint(Point3d basePoint, double angle, double offset)
  36.         {
  37.                 return new Point3d(basePoint.X + (offset * Math.Cos(angle)), basePoint.Y + (offset * Math.Sin(angle)), basePoint.Z);
  38.         }
  39. }
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

ChrisCarlson

  • Guest
Re: Use editor.Command to change an objects space.
« Reply #1 on: August 19, 2016, 10:54:40 AM »
Without running it, I don't think you need to pass "" until the end of the command

Code - C#: [Select]
  1. Active.Editor.Command("MSPACE", selectionSet, "CHSPACE", "");

Gile has a nice write up on passing autocad commands.

https://www.theswamp.org/index.php?topic=49124.0
« Last Edit: August 19, 2016, 10:58:31 AM by Master_Shake »

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Use editor.Command to change an objects space.
« Reply #2 on: August 19, 2016, 11:43:59 AM »
I guess I was not clear enough.


Code - C#: [Select]
  1. Active.Editor.Command("MSPACE");


gives the exact same error.


I am using AutoCAD 2016 but it does the same thing in 2015 also.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

ChrisCarlson

  • Guest
Re: Use editor.Command to change an objects space.
« Reply #3 on: August 19, 2016, 12:04:11 PM »
15+ changed the way commands are run, based on if you had all the arguments or required input. Just shooting from the hip, you might need to break up the two commands and have the user select the active viewport. The command MSPACE does not start and stop with that sole command, hence where I think the eInvalidInput is coming from.

Code - C#: [Select]
  1. await Active.Editor.CommandAsync("_.MSPACE", Active.Editor.PauseToken, Active.Editor.PauseToken);
  2. Active.Editor.Command("_.CHSPACE", selectionSet);


Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Use editor.Command to change an objects space.
« Reply #4 on: August 19, 2016, 12:39:05 PM »
Other commands that do not work for me.

Code - C#: [Select]
  1. Active.Editor.Command("QSAVE");
  2. Active.Editor.Command("VIEW");
  3.  


etc, etc, etc.  It does not matter what is placed in the ParamArray, it ALWAYS gives the eInvalidInput.


This is the first time that I am trying to use the API and i must not have something set correctly.  I have the 3 standard dlls referenced.  Should there be another?


At this point, I am looking into just transforming the dimensions to paperspace myself.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Use editor.Command to change an objects space.
« Reply #5 on: August 20, 2016, 01:10:21 AM »
What context are you calling from? I think it will throw error if called from application context.
...
...
...
I just did a quick test and worked in document context and threw einvalidinput error from application context.

Code - C#: [Select]
  1.         [CommandMethod("ThrowErrorMSPACE", CommandFlags.Session)]
  2.         public void ThrowErrorMSPACE()
  3.         {
  4.             Ed.Command("MSPACE");
  5.         }
  6.         [CommandMethod("SucessfulMSPACE")]
  7.         public void SucessfulMSPACE()
  8.         {
  9.             Ed.Command("MSPACE");
  10.         }
  11.  

BlackBox

  • King Gator
  • Posts: 3770
Re: Use editor.Command to change an objects space.
« Reply #6 on: August 20, 2016, 01:41:10 AM »
Out of curiosity, any reason you want to use Editor.Command() in lieu of cloning?

http://spiderinnet1.typepad.com/blog/2014/03/autocad-net-move-entity-from-model-space-to-paper-space.html


Cheers
"How we think determines what we do, and what we do determines what we get."

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Use editor.Command to change an objects space.
« Reply #7 on: August 20, 2016, 05:32:13 AM »
Jeff,

It has been a while since I've done any .Net coding, so I am just guessing.  Would it make a difference if you locked the document before calling the command in the code where you invoke the sessions command flag?
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Use editor.Command to change an objects space.
« Reply #8 on: August 20, 2016, 09:11:33 AM »
Hey Tim,

I gave it try and still got an error.
I might be doing something wrong.

Code - C#: [Select]
  1.         [CommandMethod("TryDocLockMSPACE", CommandFlags.Session)]
  2.         public void TryDocLockMSPACE()
  3.         {
  4.             using (DocumentLock docloc = Doc.LockDocument())
  5.             {
  6.                
  7.                 Ed.Command("MSPACE");
  8.             }
  9.                
  10.         }
  11.  




T.Willey

  • Needs a day job
  • Posts: 5251
Re: Use editor.Command to change an objects space.
« Reply #9 on: August 20, 2016, 12:29:21 PM »
It was worth a try, Jeff.
That was how I was envisioning it, so I cannot point out what would be wrong, if there is something wrong with the code.
Oh well.  Thanks for trying and posting back.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Use editor.Command to change an objects space.
« Reply #10 on: August 20, 2016, 12:58:55 PM »
I am unable to test right now but I believe that Jeff has the answer.  My code is ran from a button on a palette that was started with a command that has the session flag on it.  I can test later this evening to verify.  Thanks for the help everyone.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Use editor.Command to change an objects space.
« Reply #11 on: August 20, 2016, 04:45:01 PM »
Hi,

The CommandFlags.Session of the command which starts the PalatteSet is not the issue (it should be the same without this flag). A PaletteSet (as any modeless dialog) always runs in application context.
The safest (and simplest) way is to wrap the code of the button click event handler within a CommandMethod (without the CommandFlags.Session) and just call this command from the event handler with SendStringToExecute.
This way, you let AutoCAD take care of switching to document context and locking the document (and this also allows to call the command from outside the palette).
Speaking English as a French Frog