Jump to content

Viewport Position


Recommended Posts

I figured out how to scale a viewport, now I want to position it correctly.

 

The following works:

        vs.SetObjectVariableReal(vpHandle, 1003, 32) #scale

 

The following does not work:
        vs.SetObjectVariableReal(vpHandle, 1024, 0) # x
        vs.SetObjectVariableReal(vpHandle, 1025, 0) # y

 

I get the following error message:

Error: SetObjectVariable failed with constant 1024. This typically due to the operation being not supported for the passed object handle.

 

As far as I can tell in the documentation, setting the x and y should be the same as setting the scale. I can't seem to figure out why one works and the other doesn't.

 

Thanks

 

Martin

Link to comment

Martin,

   ObjVars 1024 & 1025 are marked as READ-ONLY. 

 

   Try getting the Bounding Box to find out where it is, then use HMove() to move it to where you want. The difference between the desired point and the existing point is the amount you need to move it.

 

HTH,

Raymond

Link to comment

Hello

 

perhaps this snippet from a larger script may help:

 

# -- create viewport : move viewport object to page center
vs.Layer(vLayerViewPort_name) # selects layer with ViewPort object

vViewCenter_x, vViewCenter_y = vs.GetOrigin() # this works as long SheetLayer origin was not altered
vViewPort_Rect_center_x, vViewPort_Rect_center_y = vs.HCenter(vViewPort_Object_handle)
vViewCenter_x = vViewCenter_x * -1
vViewCenter_y = vViewCenter_y * -1
vDifference_x = (vViewCenter_x-vViewPort_Rect_center_x)
vDifference_y = (vViewCenter_y-vViewPort_Rect_center_y)
# message("object_process_item : vViewCenter : " + str(vViewCenter_x) + " / " + str(vViewCenter_y) + " | " + str(vViewPort_Rect_center_x)+ " / " + str(vViewPort_Rect_center_y)  + " | " + str(vDifference_x)+ " / " + str(vDifference_y))
vObject_type = vs.GetType(vViewPort_Object_handle)
if ((vObject_type == 122) or (vObject_type == 3)): vs.HMove(vViewPort_Object_handle, vDifference_x, vDifference_y)

 

best regards

 

Link to comment

MullinRJ

 

How do you know they are read only?

 

When I look at the appendix I see the following:

Scale                           1003 REAL ObjectVariableReal

Viewport x Position  1024 REAL  ObjectVariableReal

Viewport x Position  1025 REAL  ObjectVariableReal

 

I don't see anything that would indicate 1024 and 1025 are read only. Where do you find that nugget of information.

 

Thanks

 

 

Link to comment

Martin,

   IFF [sic] you really want to GROK the whole VectorScript experience, you'll have to procure a copy of "Hitchhikers Guide to All Things VectorScript (and Python, too!)."

 

   However, it may be a bit easier to download a copy of the SDK from the VW site:  http://www.vectorworks.net/support/custom/sdk/sdkdown

and look for this file:  .../SDK/SDKVW(build#)/SDKLib/Include/Kernel/MiniCadCallBacks.h

 

   In it you will find long sloppily typed lists of extremely esoteric and somewhat arcane factoids. When you find yourself standing precisely in Ra's shadow, notice the following illuminations: 

const short ovViewportXPosition                = 1024;        // double   read only - the X coordinate of the viewport on the sheet layer - Public for VS
const short ovViewportYPosition                = 1025;        // double   read only - the Y coordinate of the viewport on the sheet layer - Public for VS
 

That is how I know 😉

 

If this doesn't help, ...

Raymond

 

 

Link to comment

Wow!

 

Nice reference! Not related, but a long time ago I have a VW bug. I found a book on VW repair. This book was so well done that at 14  I rebuilt the engine one a 1969 classic VW. It ran for years. I wish I had the same book for VW & Python scripting. Every step seems like a slugfest.  I've been banging my head against the wall all day trying to get viewport centering to work correctly.

 

Using the following code:

 

Quote

    for selectedObjects in range(0, len(objectList)):
        vs.Layer(ModuleLayer) # make sure we are on the correct starting layer

        screenName   = objectList[selectedObjects][0] # Gets the Screen Name   - 1st item in t = (screenName,objectHandle)
        objectHandle = objectList[selectedObjects][1] # Gets the Object Handle - 2nd item in t = (screenName,objectHandle)

 

        # Get the object height, width and bounding box of object
        p1, p2       = vs.GetBBox(objectHandle)
        objectWidth  = vs.HWidth(objectHandle)  
        objectHeight = vs.HHeight(objectHandle) 

 

        #Create VP Crop Object
        vs.Rect(p1, p2, objectWidth, objectHeight)
        vpCrop = vs.LNewObj()

 

        # Create the layer and set the layer name
        layerHandle = vs.CreateLayer('01', 2)  # using 01 forces the next unused sheet nubmer to be used
        layerName   = vs.GetLName(layerHandle) # get they layer name. It appears the layer name is the sheet number
        vs.SetObjectVariableString(layerHandle, 159,'Module Layout - ' + screenName)

 

        vs.Rect(0,0,10,10) # test to see if something draws on the sheet layer

 

        #Create the VP and set Crop Object
        vpHandle = vs.CreateVP(layerHandle)
        vs.SetVPCropObject(vpHandle, vpCrop)
        vs.SetObjectVariableReal(vpHandle, 1003, 32) #set scale to a default of 32

        viewPort_X, viewPort_Y = vs.HCenter(vpHandle)
        print(viewPort_X, " ", viewPort_Y)

 

    vs.ReDrawAll()


 

 

 

viewPort_X and viewPort Y are always 0.

 

What is super strange, is when I run the script it leaves me on the last sheet layer created and I can see a viewport created at 0,0 (with a red x through it). If I change sheet layers and change back, the viewport shows up in the wrong location. You can see the two attached screen shots to see what I am talking about.

 

I've tried adding ReDraw and ReDrawAll in multiple points in the code and it makes no difference. I'm using 2108 sp3.

 

It seems like some type of reset or redraw is required after the VP is created or maybe after the crop is set.

 

Any thoughts?

 

Martin

 

 

 

Screen Shot 2018-05-16 at 4.12.25 PM.png

Screen Shot 2018-05-16 at 4.11.51 PM.png

Link to comment

Gotta run, but I have 1 quick thought and no idea how it will affect viewports. Have you tried ResetObject()? This is a lot of what programming is about when you are learning, and after 30 years scripting with MC/VW I'm still learning. When in doubt, try something, anything, and take notes.

 

Good luck,

Raymond

Link to comment

Hello

 

I am using my script for producing many small plats in a batch (moving window on every object) to export each to its own pdf for many years - first in VectorScript and now ported to Python. It works without problems. I do not need to ResetObject() nor  ReDrawAll(). In general I do not see any greate difference to your code. The only difference in my script is, that I am using a predefined presentation layer and reusing it for every plat to be drawn and exported. But I do not think that this is the problem.

 

I tried and slightly adapted your code (see attachment) and the only proble that I can see is, that vs.CreateVP() creates a ViewPort-Object with no layer and class activation (they are all set to invisible). So running your code I get also this square with the red cross in it. But when I assign class and layer visibility to active for this ViewPort-Object the bounding box of the viewport change to its real dimensions and location. Thus assigning class and layer visibility is "indispensable" to get the correct result, although the square with the red cross seems to be a temporarily "artifact" with a viewport with all classes and layers set to invisible. Finally the new created viewport has to moved to the page center (or any position) - that is default and is corresponding the manual viewport creation.

 

best regards

viewport.vwx

Link to comment
  • 6 months later...

Hello

 

With VW2019 I came across, that in my example file "viewport.vwx" the Python-Script "with visibility" to generate a ViewPort and to assign to it the layer visibility to active will crash VW2019. If the created ViewPort is manually deleted VW2019 will crash.

So in the loop to assign the layer's visibility to active a condition has to assure, that the visibility is only set to design layers (many thanks to D. Corpataux from ComputerWorks.ch).

 

So in the attachment there is a new stable Script named "with viability stable VW2019".

best regards

 

viewport_v2.vwx

 


# python-script : "with visbility stable VW2019"

import vs


def createviewport(ModuleLayer, objectHandle):

	vs.Layer(ModuleLayer) # make sure we are on the correct starting layer

	# screenName   = objectList[selectedObjects][0] # Gets the Screen Name   - 1st item in t = (screenName,objectHandle)
	# objectHandle = objectList[selectedObjects][1] # Gets the Object Handle - 2nd item in t = (screenName,objectHandle)

	# Get the object height, width and bounding box of object
	p1, p2       = vs.GetBBox(objectHandle)
	objectWidth  = vs.HWidth(objectHandle)  
	objectHeight = vs.HHeight(objectHandle) 

	#Create VP Crop Object
	vs.Rect(p1, p2, objectWidth, objectHeight)
	vpCrop = vs.LNewObj()

	# Create the layer and set the layer name
	layerHandle = vs.CreateLayer('01', 2)  # using 01 forces the next unused sheet nubmer to be used
	layerName   = vs.GetLName(layerHandle) # get they layer name. It appears the layer name is the sheet number
	#vs.SetObjectVariableString(layerHandle, 159,'Module Layout - ' + screenName)
	vs.SetObjectVariableString(layerHandle, 159,'Module Layout - ' )


	# vs.Rect(0,0,10,10) # test to see if something draws on the sheet layer


	#Create the VP and set Crop Object
	vpHandle = vs.CreateVP(layerHandle)
	vs.SetVPCropObject(vpHandle, vpCrop)
	vs.SetObjectVariableReal(vpHandle, 1003, 32) #set scale to a default of 32

	## set class and layer visibility to viewport object
	vClassNum = vs.ClassNum()
	for vClassNumIndex in range(1, vClassNum+1):
		vs.SetVPClassVisibility(vpHandle, vs.ClassList(vClassNumIndex), 0)

	vLayer_handle = vs.FLayer()
	while vLayer_handle != None:
		vLayer_Type = vs.GetObjectVariableInt(vLayer_handle, 154) #Design Layer or Sheet Layer
		if vLayer_Type == 1: # Design Layer
			vs.SetVPLayerVisibility(vpHandle, vLayer_handle, 0)
		vLayer_handle = vs.NextLayer(vLayer_handle)

	viewPort_X, viewPort_Y = vs.HCenter(vpHandle)
	print(viewPort_X, " ", viewPort_Y)

  



ModuleLayer = "test"

vs.DSelectAll()
vs.SelectObj("(ALL)")
vs.Group()
vGroup_handle = vs.LSActLayer()

createviewport(ModuleLayer, vGroup_handle)

vs.HUngroup(vGroup_handle)

 

  • Like 1
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...