Thank you for the reply! Any tip is greatly appreciated.
GetMouse() is not accurate for my application. I think I understand what PenLoc() is meant for therefore it won't work. I scrapped this script and changed the approach. What I was looking for was reference lines to aid with creation of new items. With a shortcut. Next day I found the Guides under the Modify menu. Awesome.
So I tried a new script, that would take a simple line positioned with precision and direction, and assigned it to Guides class, achieving my goal precisely. ChatGPT is atrocious with Vectorworks, yet it worked well with Python. Again, any correction is welcome.
What is "Marionette Maven"?
import vs
import math
# @florin, 09/12/2023
# find the page dimensions
current_layer = vs.ActLayer()
page_width, page_height = vs.TBB_GetPageArea(current_layer)
pw = page_width * 24.5 # translate to mm
ph = page_height * 24.5 # translate to mm
# remember existing active class, go back to it when done with the construction line
active_class = vs.ActiveClass()
counter = 0 # not used
message = "" # not used
# per chatgpt, find angle between two points
# where each point has an x/y coordinates set
# these x/y coordinates are given by vw, via vs.GetSegPt1, vs.GetSegPt2
def find_angle(p1, p2):
x1, y1 = p1
x2, y2 = p2
radians = math.atan2(y2 - y1, x2 - x1)
return math.degrees(radians)
# per chatgpt, project next x/y location given angle and distance
# distance is calculated using page dimensions
def project_point(x1, y1, angle_degrees, distance):
# Convert angle from degrees to radians
angle_radians = math.radians(angle_degrees)
# Calculate the new x and y coordinates
x2 = x1 + distance * math.cos(angle_radians)
y2 = y1 + distance * math.sin(angle_radians)
return x2, y2
def class_change_callback(obj):
type = vs.GetTypeN(obj)
global counter
global message
#types = types + ",{}".format(type)
counter += 1
if type == 2: # 2 is type for line
# get starting and ending coordinates
cx, cy = vs.GetSegPt1(obj)
dx, dy = vs.GetSegPt2(obj)
# angles go in opposite directions
angle1 = find_angle((cx,cy),(dx,dy))
angle2 = find_angle((dx,dy),(cx,cy))
#message = "{},{}".format(angle1,angle2)
# start x,y and end x,y of the line
sx, sy = project_point(cx, cy, angle1, (pw*ph)/2)
ex, ey = project_point(dx, dy, angle2, (pw*ph)/2)
vs.SetSegPt1(obj, sx, sy) # set next x/y in oner direction
vs.SetSegPt2(obj, ex, ey) # set next x/y in oposite direction
red = vs.RGBToColorIndex(65535, 0, 0) # red color
vs.SetPenFore(obj, red) # set color of line
vs.SetLW(obj, 1) # line weight
vs.SetClass(obj, "Guides")
vs.DoMenuTextByName("Lock",0)
vs.SetDSelect(obj) # deselect the line
# Iterate through all objects in the document and apply the callback function
# vs.ForEachObject(class_change_callback, "INSYMBOL & INOBJECT & INVIEWPORT & (VSEL=TRUE)")
vs.ForEachObjectInLayer(class_change_callback, 3, 0, 4); #{ Visible/Selected objects, Shallow, Editable Layers }
#vs.Message("{}".format(message))
# revert to originally active class
vs.NameClass(active_class)
vs.SetTool(-240) # ensure Selection tool is active