Hi,
I have been selecting Mtext using Selection Filter,
Then I have been using command "CHSPACE" to bring Selected Mtext to Paper Space,
But when I send Command "CHSPACE", it has been again asking to select Objects,
Could any help how to change my code to achieve this.
Dim acTypValAr(1) As TypedValue acTypValAr.SetValue(New TypedValue(DxfCode.Start, "MTEXT"), 0) acTypValAr.SetValue(New TypedValue(DxfCode.Visibility, 0), 1) Dim filters = New SelectionFilter(acTypValAr) Dim sels As PromptSelectionResult sels = ed.SelectAll(filters) Dim acSSet As SelectionSet = sels.Value ThisDrawing.SendCommand("CHSPACE" + vbCr)
Thanks
Solved! Go to Solution.
Solved by _gile. Go to Solution.
Hi,
It looks like you're mixing the .NET API (Editor.SelectAll()) and the COM API (AcadDocument.SenCommand()).
You cannot directly pass a .NET)selection set to SendCommad(). This method only accepts a string as argument (as the .NET SendStringToExecute() one).
If you're targeting AutoCAD 2015 or later, you can use the Editor.Command() method which mainly works as the command LISP function and can accept a SelectionSet instance as argument.
ed.Command("_chspace", acSSet.Value, "")
If you're tageting prior versions of AutoCAD you can use some RunCommand wrapper as the one provided by Tony Tanzillo.
Hi Gile,
Many Thanks,
I have been Using AutoCAD 2016,
When I run the Code, I am getting error as "eInvalidInput"
Also attached the Screen Short.
Can you pls tell what correction I need?
Thanks
I have tried to automate the CHSPACE command in the past and it has proved difficult. The command requires a selection set and it also requires you to be inside a viewport to make the selection. It is far easier to just transform the entities using the .TransformBy method of the entity and let .NET move it for you.
Going from paperspace to model space is easy as it is a 1:1 relationship. There is only 1 point in model space that matches up with the point in paperspace. Going from modelspace to paperspace is not as easy as there could be an unlimited amount of viewports so it is not a 1:1 relationship. So you need a transform that is specific to your viewport. You should already have the viewport somewhere in your code as the CHSPACE command requires it or at least requires that you be in model space inside a viewport in order to work.
You could then use the code below to get the model to paperspace transform. If you search the autocad help files you will find this code in there somewhere.
/// <summary> /// Gets the model to paper transform. /// </summary> /// <param name="viewport">The viewport.</param> /// <returns>A Matrix3d.</returns> /// <exception cref="System.NotSupportedException">Perspective views not supported</exception> public static Matrix3d GetModelToPaperTransform(this Viewport viewport) { if (viewport.PerspectiveOn) { throw new NotSupportedException("Perspective views not supported"); } Point3d center = new Point3d(viewport.ViewCenter.X, viewport.ViewCenter.Y, 0.0); return Matrix3d.Displacement(new Vector3d(viewport.CenterPoint.X - center.X, viewport.CenterPoint.Y - center.Y, 0.0)) * Matrix3d.Scaling(viewport.CustomScale, center) * Matrix3d.Rotation(viewport.TwistAngle, Vector3d.ZAxis, Point3d.Origin) * Matrix3d.WorldToPlane(new Plane(viewport.ViewTarget, viewport.ViewDirection)); }
Now you can just transform the entity by the resulting matrix and then add it to the layout that contains the viewport.
@Keith.Brown wrote:You could then use the code below to get the model to paperspace transform. If you search the autocad help files you will find this code in there somewhere.
Would you happen to know where in the help files that code appears?
Hi gile,
Selection is not Empty, but still getting the Same error as "eInvaildInput"
attached the image 33,
Hi Keith.Brown,
I was not familiar with ".Transformby" Method,
But Still I tried, could you pls guied me how to change code to achive the Result,
Using lock As DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument Using trs As Transaction = db.TransactionManager.StartTransaction() For Each item In sels.Value.GetObjectIds() Dim entitys As Entity = DirectCast(trs.GetObject(item, OpenMode.ForWrite), Entity) Dim vpid As ObjectId = ed.ActiveViewportId Dim oViewport As Viewport = trs.GetObject(vpid, OpenMode.ForWrite) Call GetModelToPaperTransform(oViewport) entitys.TransformBy() Next trs.Commit() End Using End Using
This is actually code from Tony Tanzillo's ViewportExtensionMethods class. I had the documentation at the top of my class but not in the actual method itself so I did not see it right away.
This post over at the swamp shows the original code and some explanation.
https://www.theswamp.org/index.php?topic=42503.msg477118#msg477118
It appears that based on the code that you have shown and your question that you need to brush up on your .NET skills.
The method that I shared is an extension method of the viewport object that returns a matrix3d. You are calling it as a Sub in VB.NET. So... I will try to explain it.
First, there is no need for the keyword CALL. It is not .NET but is instead from Visual Basic which is different so just invoke the method by its name. The CALL is not needed.
Since GetModelSpaceToPaperSpaceTransform is a function, you need to assign it to a variable like so.
Dim mat As Matrix3d = oViewport.GetModelToPaperSpaceTransform()
Then you use mat to transform the entity. You are calling TransformBy and not passing in any value at all.
entitys.TransformBy(mat)
I would really recommend learning the basics of .NET more before trying AutoCAD.NET API. It is much more difficult and made even more difficult by sketchy documentation.
If you are just beginning then I would also consider moving to C# instead of VB.NET. There are much more examples in C# than in VB in the AutoCAD World.
Are you absolutely certain the selection set is not empty (i.e. you put a break point to check the acSSet contents)?
This works for me:
using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; using AcAp = Autodesk.AutoCAD.ApplicationServices.Core.Application; [assembly: CommandClass(typeof(ChangeSpaceSample.Commands))] namespace ChangeSpaceSample { public class Commands { [CommandMethod("TEST", CommandFlags.Modal | CommandFlags.NoTileMode)] public void Test() { var ed = AcAp.DocumentManager.MdiActiveDocument.Editor; ed.SwitchToModelSpace(); var filter = new SelectionFilter(new[] { new TypedValue(0, "MTEXT"), new TypedValue(60, 1) }); var selection = ed.SelectAll(filter); if (selection.Status == PromptStatus.OK) ed.Command("_chspace", selection.Value, ""); } } }
VB conversion:
Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.EditorInput Imports Autodesk.AutoCAD.Runtime Imports AcAp = Autodesk.AutoCAD.ApplicationServices.Core.Application <Assembly: CommandClass(GetType(ChangeSpaceSample.Commands))> Namespace ChangeSpaceSample Public Class Commands <CommandMethod("TEST", CommandFlags.Modal Or CommandFlags.NoTileMode)> _ Public Sub Test() Dim ed = AcAp.DocumentManager.MdiActiveDocument.Editor ed.SwitchToModelSpace() Dim filter = New SelectionFilter(New TypedValue() {New TypedValue(0, "MTEXT"), New TypedValue(60, 1)}) Dim selection = ed.SelectAll(filter) If selection.Status = PromptStatus.OK Then ed.Command("_chspace", selection.Value, "") End If End Sub End Class End Namespace '======================================================= 'Service provided by Telerik (www.telerik.com) 'Conversion powered by NRefactory. 'Twitter: @telerik 'Facebook: facebook.com/telerik '=======================================================
In addition to what @Keith.Brown wrote, extension methods are handled differently in VB.NET.
They must be placed in a Module rather than in a Class. If you don't do that, then you can use
them as extension methods.
Hi All,
Many Thanks For All Your guidance...
Highly thankful for your help...
Can't find what you're looking for? Ask the community or share your knowledge.