Jump to content

Center Drawing on Origin


Recommended Posts

Patrick,

   When in doubt, move it yourself.

PROCEDURE CenterOnOrigin;
{ Moves everything on a design layer to the Origin, }
{ then moves the User Origin to maintain the same XY readings. }
{ 11 Jul 2016 - Raymond J Mullin }

{ USE AT YOUR OWN RISK, and USE on a COPY of your data FIRST! } 

VAR
x, y : Real;
Orig, Pcen, V :Vector;
BEGIN
GetOrigin(Orig.x, Orig.y);
SelectAll;
Group;
hCenter(FSActLayer, Pcen.x, Pcen.y);
V := -Pcen - Orig;	{ offset vector }
hMove(FSActLayer, V.x, V.y);
UnGroup;

{ optional, to retain the same x&y after the move }
SetOrigin(V.x, V.y);	{ remove to keep drawing center unchanged }

END;
Run(CenterOnOrigin);

Raymond

Link to comment

OOPS! This is the Python Scripting Corner.

Try this instead:

# CenterOnOrigin
# Moves everything on a design layer to the Origin, 
# then moves the User Origin to maintain the same XY readings. 
# 11 Jul 2016 - Raymond J Mullin 
# USE AT YOUR OWN RISK, and USE on a COPY of your data FIRST!  

import vs

(OrigX, OrigY) = vs.GetOrigin()
vs.SelectAll()
vs.Group()
(PcenX, PcenY) = vs.HCenter(vs.FSActLayer())
Vx = -PcenX - OrigX
Vy = -PcenY - OrigY
vs.HMove(vs.FSActLayer(), Vx, Vy)
vs.Ungroup()

# optional, to retain the same x&y after the move
vs.SetOrigin(Vx, Vy);	# remove to keep drawing center unchanged

Raymond

Link to comment

In the previous script I took a more readable approach to presenting the variable elements. In this version I use some of Python's built in functionality.

Note: the use of tuples and lists to represent "vectors" and the "zip" function to add the vector elements.

In all three versions of this script, if the User Origin is not at the Absolute Origin, this script will adjust for that.

# CenterOnOrigin
# Moves everything on a design layer to the Origin, 
# then moves the User Origin to maintain the same XY readings. 
# 11 Jul 2016 - Raymond J Mullin 
# USE AT YOUR OWN RISK, and USE on a COPY of your data FIRST!  

import vs

vs.SelectAll()
vs.Group()

# Orig, Pcen are tuples representing (X,Y) vectors
Orig = vs.GetOrigin()
Pcen = vs.HCenter(vs.FSActLayer())
V = [-x-y for x,y in zip(Pcen-Orig)]	# offset vector V = -Pcen-Orig

vs.HMove(vs.FSActLayer(), V[0], V[1])
vs.Ungroup()

# optional, to retain the same x&y after the move
vs.SetOrigin(V[0], V[1]);	# remove to keep drawing center unchanged

Raymond

Link to comment
OOPS! This is the Python Scripting Corner.

Try this instead:

# CenterOnOrigin
# Moves everything on a design layer to the Origin, 
# then moves the User Origin to maintain the same XY readings. 
# 11 Jul 2016 - Raymond J Mullin 
# USE AT YOUR OWN RISK, and USE on a COPY of your data FIRST!  

import vs

(OrigX, OrigY) = vs.GetOrigin()
vs.SelectAll()
vs.Group()
(PcenX, PcenY) = vs.HCenter(vs.FSActLayer())
Vx = -PcenX - OrigX
Vy = -PcenY - OrigY
vs.HMove(vs.FSActLayer(), Vx, Vy)
vs.Ungroup()

# optional, to retain the same x&y after the move
vs.SetOrigin(Vx, Vy);	# remove to keep drawing center unchanged

Raymond

thanks for your effort! Works great. (The Example from the 3rd post crashes.)

Edited by Patrick Winkler
Link to comment

The Script caused an Dialog ('The Group is in a class. Do you really want to ungroup...') to show up. In order to prevent the dialog I just set the None class active at the begin of your function. SetClass would also change the class of all objs in the group!


# for germany 
#CLASS_NONE = 'Keine'

CLASS_NONE = 'None'

vs.NameClass (CLASS_NONE)

Edited by Patrick Winkler
Link to comment

That makes sense. I almost always have "None" as my active class. That's something I would easily miss. Actually, all three scripts will suffer this problem, since they all work pretty much the same way. Nice catch, Patrick.

If you want the script to be more fluid, save the active class at the beginning and restore it at the end.

Try this expression to see if it works across language lines.

vs.NameClass(vs.Index2Name(2))

I am curious if it will return 'Keine' in your version of VW. Please let me know as I cannot test it here.

   index 2 in the NameList is always "None" and

   index 3 is always "Dimension"

My assumption is that when VW is ported to another language, this call will return "None" in the local language. If it works as I expect, there will be no need comment out lines for different international users.

There is another mistake in the 3rd example:

V = [-x-y for x,y in zip(Pcen-Orig)]   # offset vector V = -Pcen-Orig

should read:

V = [-x-y for x,y in zip(Pcen, Orig)]   # offset vector V = -Pcen-Orig

Typo. There should be a comma between the arguments in "zip", and not a minus sign.

Raymond

Link to comment

Howdy,

tought the same but None is not recocnized as the None class in the germ version.

Yes vs.Index2Name(2) gives me 'Keine'.

Just found another flaw.

The coordinates of objects on other layers are changed by the script.

In order to prevent that I just move erverything in the doc. This hasn't been tested well!


def center_drawing_on_internal_origin ():
   '''
        Moves everything on the active layer to the Origin, 
        then moves the User Origin to maintain the same XY readings.

        11 Jul 2016 - Raymond J Mullin
   '''      

   def get_None_class (): return vs.Index2Name(2)  
   vs.NameClass (get_None_class()) # Klasse keine aktiv setzen

   (OrigX, OrigY) = vs.GetOriginInDocUnits ()   

   # Group all objs on the active Layer
   vs.SelectAll() # ??? Select / Get all Objs in the doc?
   vs.Group()
   group_h = vs.LActLayer()

   (group_cen_X, group_center_Y) = vs.HCenter (group_h)

   Vx = -group_cen_X - OrigX
   Vy = -group_center_Y - OrigY    

   vs.Ungroup()

   def move (h): vs.HMove( h, Vx, Vy)

   ForEachObject (move, 'ALL')

   # Optional, to retain the same x&y after the move    
   vs.SetOrigin (Vx, Vy);    # remove to keep drawing center unchanged

Link to comment

Howdy Patrick,

   That's mighty Texan of you  ;-)   I think you are on the right track with: vs.ForEachObject().

   I didn't really look at how you got your "group_center", but you can easily extract a virtual BoundingBox with:

X1 = vs.LeftBoundN("All")

X2 = vs.RightBoundN("All")

Y1 = vs.TopBoundN("All")

Y2 = vs.BotBoundN("All")

for all objects in the drawing,

or use:

X1 = vs.LeftBoundN("V")

etc., to only process visible objects.

then:

Xc = (X1 + X2) / 2

Yc = (Y1 + Y2) / 2

(-Xc, -Yc) is the amount you will move everything, assuming the user origin was not moved. Otherwise, you'll have to do the math we did earlier.

   With different constraints, come different solutions.

HTH,

Raymond

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...