.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to run "_scale" command using SendStringToExecute

4 REPLIES 4
Reply
Message 1 of 5
seiya.kumada
537 Views, 4 Replies

How to run "_scale" command using SendStringToExecute

 

 

We are now trying to execute the command "_scale" which is passed to the function "SendStringToExecute" in C# code.
The string we made is as follows;

 

var command = string.Format("_SCALE\n {0}\n {1}\n R\n {2} {3}\n {4}\n", rasterImageId, basePoint, firstPointString, secondPointString, unitFactor); 

var document = Application.DocumentManager.MdiActiveDocument;
document.SendStringToExecute(command, true, false, false);

 

where rasterImageId, basePoint, firstPointString, secondPointString, and unitFactor are given by;

 

// rasterImageId
var rasterImageId = blockTableRecord.AppendEntity(rasterImage);

 

// basePoint
var basePoint = (new Autodesk.AutoCAD.Geometry.Point2d(imagePlacementBasePoint.X, imagePlacementBasePoint.Y)).ToString().TrimEnd(')');
basePoint = basePoint.TrimStart('(');

 

// firstPoinString
var firstPointString = (new Autodesk.AutoCAD.Geometry.Point2d(imagePlacementBasePoint.X, imagePlacementBasePoint.Y)).ToString().TrimEnd(')');
firstPointString = firstPointString.TrimStart('(');

 

// secondPointString
var secondPointString = (new Autodesk.AutoCAD.Geometry.Point2d(imagePlacementBasePoint.X + 1.0, imagePlacementBasePoint.Y)).ToString().TrimEnd(')');
secondPointString = secondPointString.TrimStart('(');

 

// unitFactor
var unitFactor = 31.3;

 

Unfortunately, the above command doesn't work.

Anyone have any advice on how we should do this?

 

Thanks in advance.

Seiya Kumada

4 REPLIES 4
Message 2 of 5
norman.yuan
in reply to: seiya.kumada

Since you did not show entire code (e.g. the code before and after calling SendStringToExecute()), it is difficult to say why. But if you have code that follows SendStringToExecute(), then that most likely is the reason, because SendStringToExecute() is executed asynchonously, and the the code follows could get executed first and make the "SCALE" coomand's working context changed.

 

You really should use API to transform (scaling, rotating, moving...) entity (a RasterImage is an Entity, right?) instead of sending command string.

 

Following sample code scales an image entity easily:

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(ScaleImage.MyCadCommands))]

namespace ScaleImage
{
    public class MyCadCommands
    {
        [CommandMethod("ImgScale")]
        public static void RunMyCadCommand()
        {
            Document dwg = CadApp.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;

            try
            {
                ObjectId id = PickImageEntity(ed);
                if (id.IsNull)
                {
                    ed.WriteMessage("\n*Cancel*");
                }
                else
                {
                    ScaleImage(id, 2.0);
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\nError: {0}\n{1}", ex.Message, ex.StackTrace);
            }
            finally
            {
                Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
            }
        }

        private static ObjectId PickImageEntity(Editor ed)
        {
            PromptEntityOptions opt = new PromptEntityOptions(
                "\nSelect a raster image:");
            opt.SetRejectMessage("\nInvalid: not raster image.");
            opt.AddAllowedClass(typeof(RasterImage), true);
            PromptEntityResult res = ed.GetEntity(opt);
            if (res.Status == PromptStatus.OK)
                return res.ObjectId;
            else
                return ObjectId.Null;
        }

        private static void ScaleImage(ObjectId id, double scale)
        {
            using (var tran=id.Database.TransactionManager.StartTransaction())
            {
                RasterImage img = (RasterImage)tran.GetObject(id, OpenMode.ForWrite);

                Point3d basePt = img.Position;
                Matrix3d mt = Matrix3d.Scaling(scale, basePt);

                img.TransformBy(mt);

                tran.Commit();
            }
        }
    }
}

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 5
seiya.kumada
in reply to: norman.yuan

Dear norman.yuan

 

Thank you for your reply.

We appriate it.

 

Our goal is to display a small image (e.g. 640x640 pixels) on the model space and to show to users the length

that is modified by program in C#.

For example, when a user picks any two points on the image and measures the distance between them,

we give him the modfied length, which might have a unit [m], but real one which has pixels.

 

We refred to this site:

http://www.cadtutor.net/tutorials/autocad/scaling-images.php

The site describes how to modify the scale by means of "_SCALE" command.

It accomplished that though an image is small, the length between two points a user select is big.

 

If anyone has information concerning about a way to make "small image but big length" happen,

please let us know.

 

Sincerely yours,

Seiya Kumada 

 

 

Message 4 of 5
norman.yuan
in reply to: seiya.kumada

Well, the point is that you should use the API to scale the image rather tahn use SendStringToExecute() in most case, if you are PROGRAMMING.

 

As for your need (and the article you referred to), it is rather easy to calulate the scale based on the points picked, the image size and insertion scale. It is just a simple math issue.

Norman Yuan

Drive CAD With Code

EESignature

Message 5 of 5
seiya.kumada
in reply to: norman.yuan

Dear norman.yuan

 

Thank you for your reply.

We will study the URL you offered.

 

S.Kumada

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost