Author Topic: Transforming Points in model for calculating Viewports center, width height  (Read 1781 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6150
I really not sure what the correct terms to use here so will give an example using drawings from autocad samples

So you take your titleblock and create a annotative block out of it or insert in model space and scale it up to match viewports scale.

So in pic titleblock is scaled by 96 for 1/8" = 1-0'



Lets say the user is prompted to select the window of the viewport in model space to be created in paperspace.
So they select the 2 corners of the yellow rectangle.
So I have the information for settings the Viewports view properties, like Viewport.ViewCenter, etc..., and to calculate its width and height,
but how would I calculate its CenterPoint?
The bottom left corner of titleblock(green rectangle) is 0,0 in paperspace.

Would create viewport in paperspace



Make sense?

kaefer

  • Guest
So I have the information for settings the Viewports view properties, like Viewport.ViewCenter, etc..., and to calculate its width and height,
but how would I calculate its CenterPoint?

I'm not sure you have to do that. Let's assume ViewDirection = Vector3d.ZAxis and also UCS = WCS (and let's ignore the block insertion point too), then it seems to be sufficient just to set ViewCenter. The block reference is cloned and placed in paperspace relative to the mid point between the two selected corners, which is paperspace origin, and the center point of the Viewport too.

Excuse the choice of language.

Code - F#: [Select]
  1. [<CommandMethod("BLTRPS", CommandFlags.UsePickSet)>]
  2. let blockTransPSCmd() =
  3.   let doc = acadApp.DocumentManager.MdiActiveDocument
  4.   let db = doc.Database
  5.   let ed = doc.Editor
  6.   let peo = PromptEntityOptions("Select block reference")
  7.   peo.SetRejectMessage "Only a block reference allowed."
  8.   peo.AllowNone <- true
  9.   peo.AddAllowedClass(typeof<BlockReference>, false)
  10.   let per = ed.GetEntity peo
  11.   if per.Status = PromptStatus.OK then
  12.     let ppo = PromptPointOptions("Pick first corner point")
  13.     let ppr0 = ed.GetPoint ppo
  14.     if per.Status = PromptStatus.OK then
  15.       ppo.Message <- "Pick second corner point"
  16.       let ppr1 = ed.GetPoint ppo
  17.       if per.Status = PromptStatus.OK then
  18.         use tr = db.TransactionManager.StartTransaction()
  19.         let br = tr.GetObject(per.ObjectId, OpenMode.ForRead) :?> BlockReference
  20.         let mp0, mp1 = ppr0.Value, ppr1.Value
  21.         let mcen = mp0 + (mp1 - mp0) / 2.
  22.         // Rotation and scaling from mspace to pspace
  23.         let ms2ps =
  24.             Matrix3d.Displacement(Point3d.Origin - mcen) *
  25.             Matrix3d.Scaling(1. / br.ScaleFactors.X, mcen) *
  26.             Matrix3d.Rotation(-br.Rotation, Vector3d.ZAxis, mcen) *
  27.             Matrix3d.WorldToPlane(Vector3d.ZAxis)
  28.         let pp0, pp1 = mp0.TransformBy ms2ps, mp1.TransformBy ms2ps
  29.         let size = pp1 - pp0
  30.         // Rotation for viewcenter from pspace to mspace
  31.         let vcen3 =
  32.             mcen.TransformBy <|
  33.                 Matrix3d.Rotation(-br.Rotation, Vector3d.ZAxis, Point3d.Origin)
  34.         let bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) :?> BlockTable
  35.         let ps = tr.GetObject(bt.[BlockTableRecord.PaperSpace], OpenMode.ForWrite) :?> BlockTableRecord
  36.         let brnew = br.Clone() :?> BlockReference
  37.         brnew.TransformBy ms2ps
  38.         ps.AppendEntity brnew |> ignore
  39.         tr.AddNewlyCreatedDBObject(brnew, true)
  40.         use vp = new Viewport(Width = abs size.X, Height = abs size.Y)
  41.         let vpId = ps.AppendEntity vp
  42.         tr.AddNewlyCreatedDBObject(vp, true)
  43.         tr.Commit()
  44.         // Viewport has to be added to database
  45.         use tr = db.TransactionManager.StartTransaction()
  46.         let vp = tr.GetObject(vpId, OpenMode.ForWrite) :?> Viewport
  47.         vp.On <- true
  48.         vp.ViewCenter <- Point2d(vcen3.X, vcen3.Y)
  49.         // Rotation and scaling of viewport
  50.         vp.TwistAngle <- -br.Rotation
  51.         vp.ViewHeight <- abs size.Y * br.ScaleFactors.X
  52.         tr.Commit()

Edit: Fixed issue with scaling
« Last Edit: March 01, 2014, 04:17:07 AM by kaefer »

Jeff H

  • Needs a day job
  • Posts: 6150
Thanks kaefer,

Been really sick but will get chance today or tomorrow to where I can sit up long enough without throwing up to look at it.

sybold

  • Newt
  • Posts: 62
 :-o

been trying to work this out last year, and failed, now i need time to try again.

Jeff H

  • Needs a day job
  • Posts: 6150
ahhh,

Thanks kaefer,

Was making that harder than it should be.