Author Topic: How to scale and entity by different x and y factors?  (Read 5902 times)

0 Members and 1 Guest are viewing this topic.

Chumplybum

  • Newt
  • Posts: 97
How to scale and entity by different x and y factors?
« on: November 25, 2008, 06:06:47 PM »
hi all, my lack of knowledge of matrixis and vectors has finally caught up with me...

i'm trying to scale an entity with diffierent x and y factors and have been unable to get it to work, in my case i'm trying to scale a databaseservices.rasterimage entity (although i have been unsuccessful in scaling any entity)

below is the code i'm using so far (but not working), i've also tried matrix3d.scale but this scales both the x and y together

Code: [Select]

                    Dim SelectedImage As DatabaseServices.RasterImage = Me.Transaction.GetObject(Result.ObjectId, DatabaseServices.OpenMode.ForRead)

                    Dim NewScaleMatrix As Scale3d = New Scale3d(10, 20, 0)

                    If SelectedImage.Orientation.Xaxis.Length <> Image.Width Then
                        SelectedImage.UpgradeOpen()
                        SelectedImage.TransformBy(NewScaleMatrix.GetMatrix)
                        SelectedImage.DowngradeOpen()
                    End If


any help would be great

cheers, mark

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2121
  • class keyThumper<T>:ILazy<T>
Re: How to scale and entity by different x and y factors?
« Reply #1 on: November 26, 2008, 12:13:36 AM »

Hi Mark

The DatabaseServices.RasterImage seems to only have a single 'scale' property  ... don't know if you can actually scale with differing x and y values.

.. don't have time to play .. sorry I cant be of any real help.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Chumplybum

  • Newt
  • Posts: 97
Re: How to scale and entity by different x and y factors?
« Reply #2 on: November 26, 2008, 01:52:23 AM »
thanks, i did have a look at that but the 'scale' property is readonly

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: How to scale and entity by different x and y factors?
« Reply #3 on: November 26, 2008, 05:57:15 AM »
I did try to offer some advise before but I was a little too quick on the trigger finger, after reading again though, does the Matrix3d have a scaleBy method or similar?
If the raster image is classed a database 'entity' proper, I would think that it can be scaled as well.
Basically the raster image at it's lowest level is a polygon with an image 'mapped' to it. What this means is that if the polygon is 4X3 units say and the image is 3X3, if you map it to the polygon it will stretch it.
But, I'm thinking, if you insert a raster image it dictates it's scale in x and y to keep it in proportion, but maybe if you transform it by a scaling matrix it may 'stretch' the polygon it's mapped to and therefore stretch the image.
Might be something to look at anyway, the point is you may have to dig a little deeper as the raster scaling is trying to keep the image in prortion as inserted.
hth,
Mick.
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

Glenn R

  • Guest
Re: How to scale and entity by different x and y factors?
« Reply #4 on: November 26, 2008, 07:57:14 AM »
Matrix.Scaling(blahblah) is what you want, however it only takes one double argument for the scale, which means you won't be able to use a non-uniform scale, as your example above shows, because the image locks the aspect ratio I believe.

IIRC, I believe I've done this in the editor once or twice over the years, by putting the image in a block and distorting the scale of the block reference instead.

Bryco

  • Water Moccasin
  • Posts: 1882
Re: How to scale and entity by different x and y factors?
« Reply #5 on: November 26, 2008, 10:37:57 AM »
eCannotScaleNonUniformly is all I get.
Code: [Select]
public static Matrix3d scaleMatrix(double x,double y,double z,Point3d cen)
        {
            if (x == y && x == z) return Matrix3d.Scaling(x, cen);
            double[] data={x,0,0,cen.X,0,y,0,cen.Y,0,0,z,cen.Z,0,0,0,1};
            Matrix3d m = new Matrix3d(data);
            return m;
        }

Glenn R

  • Guest
Re: How to scale and entity by different x and y factors?
« Reply #6 on: November 26, 2008, 10:40:21 AM »
...for the matrix construction or trying to scale an image with a non-uniform matrix?

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: How to scale and entity by different x and y factors?
« Reply #7 on: November 26, 2008, 03:26:32 PM »
Good idea Glenn!
That's sorta why I figured you could set up a non orthographic matrix, at the end of the day, you could probably build your own matrix anyway as it's only an array of doubles, perhaps you can derive your own Matrix3d class (MyMatrix3d : Matrix3d) and build the values in their to pass as an object??

I'm only working from memory here but when you pass the matrix to the method, the method extracts the matrix array and does it's mojo....somewhere anyway :)
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

Bryco

  • Water Moccasin
  • Posts: 1882
Re: How to scale and entity by different x and y factors?
« Reply #8 on: November 26, 2008, 06:12:36 PM »
Quote
...for the matrix construction or trying to scale an image with a non-uniform matrix?
I tried this on a block (with a circle in it), a circle and a rectangle. It gave the same error-eCannotScaleNonUniformly on each.
Of course a circle was a silly object to pick as it becomes an ellipse.
As far as I can tell the matrix is correct (I compared it against blocks that I had scaled non uniformly  in the properties window.), so Mick I don't think it can be done with a straight transform.
I didn't get time to try it on a raster, but in the past I have always added it to a block as mentioned by Glenn.

I think NonUniforml scaling involves a rewrite of each object (circle->ellipse etc) however a blockrefernce must hold some weird xdata to achieve this

Chumplybum

  • Newt
  • Posts: 97
Re: How to scale and entity by different x and y factors?
« Reply #9 on: November 26, 2008, 07:38:09 PM »
thanks glenn, looks like creating a block and scaling it is the way to go


cheers, Mark

TonyT

  • Guest
Re: How to scale and entity by different x and y factors?
« Reply #10 on: November 26, 2008, 10:50:12 PM »
hi all, my lack of knowledge of matrixis and vectors has finally caught up with me...

i'm trying to scale an entity with diffierent x and y factors and have been unable to get it to work, in my case i'm trying to scale a databaseservices.rasterimage entity (although i have been unsuccessful in scaling any entity)


Code: [Select]

public static class Class1
{
   /// <summary>
   ///
   /// Scale a raster image reference non-uniformly.
   ///
   /// </summary>

   [CommandMethod( "SCALEIMAGE" )]
   public static void ScaleImageCommand()
   {
      Document document = Acad.DocumentManager.MdiActiveDocument;
      Editor ed = document.Editor;
      if( SystemObjects.RuntimeClasses["AcDbRasterImage"] == null )
      {
          ed.WriteMessage("\nThere are no images in this drawing.");
          return;
      }
      PromptEntityOptions peo = new PromptEntityOptions( "\nSelect raster image: " );
      peo.SetRejectMessage( "\nRequires a raster image" );
      peo.AddAllowedClass( typeof( RasterImage ), false );
      PromptEntityResult per = ed.DoPrompt( peo ) as PromptEntityResult;
      if( per.Status == PromptStatus.OK )
      {
         using( Transaction tr = document.TransactionManager.StartTransaction() )
         {
            RasterImage image = tr.GetObject( per.ObjectId, OpenMode.ForRead ) as RasterImage;
            if( image == null )
               return;

            PromptDoubleOptions pdo = new PromptDoubleOptions( "\nX Scale factor:" );
            pdo.AllowNegative = false;
            pdo.AllowZero = false;
            pdo.DefaultValue = 1.0;
            pdo.UseDefaultValue = true;
            PromptDoubleResult pdr = ed.GetDouble( pdo );
            if( pdr.Status != PromptStatus.OK )
               return;
            double xscale = pdr.Value;
            pdo.Message = "\nY Scale factor: ";
            pdo.DefaultValue = pdr.Value;
            pdr = ed.GetDouble( pdo );
            if( pdr.Status != PromptStatus.OK )
               return;
            double yscale = pdr.Value;
            if( xscale == 1.0 && yscale == 1.0 )
               return;

            image.UpgradeOpen();

            image.Orientation = new CoordinateSystem3d(
               image.Orientation.Origin,
               image.Orientation.Xaxis * xscale,
               image.Orientation.Yaxis * yscale );

            tr.Commit();
         }
      }
   }
}


« Last Edit: December 02, 2008, 09:22:08 PM by TonyT »