Hi,
So in the code, you need a way to automatically identify the textobject to set the scale text to.
Currently you have it working so that you can select the text and have the text changed.
What would be a way for the code to identify the text object?
Suppose you name the textobject ‘detailscaletext’ than after changing the detail scale the script below searches for text objects on the current layout that are named ‘detailscaletext’ and sets the variable scale as the new text:
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
def set_namedtexts_on_active_layout(OBJ_NAME, NEW_TEXT):
"""
find all text objects named OBJ_NAME that are on the current layout
and set their text to NEW_TEXT
"""
#collect all texts named 'detailscale'
all_named = rs.ObjectsByName(OBJ_NAME)
all_namedtexts = [id for id in all_named if rs.IsText(id)]
#get the id of the currently active layout view
layout_id = rs.CurrentView(return_name=False)
for scale_text_id in all_namedtexts:
#get the rhino object from the id and check what viewport/layout id it is assigned to
rhino_obj = rs.coercerhinoobject(scale_text_id)
text_view_id = rhino_obj.Attributes.ViewportId
#check if the layout_id equals the text object viewport id
if layout_id == text_view_id:
#set the text to the new_text
rs.TextObjectText(scale_text_id, NEW_TEXT)
def setDetailScale():
"""
this script changes the scale of a detail to a selected scale from a listbox
version 1.1
www.studiogijs.nl
"""
detail = rs.GetObject("select detail", filter = 32768)
if not detail:
return
detail = rs.coercerhinoobject(detail)
if not detail.DetailGeometry.IsParallelProjection:
return
#modify the scale
items = ["1/16\"=1'-0\"", "1/8\"=1'-0\"", "3/16\"=1'-0\"", "1/4\"=1'-0\"", "3/8\"=1'-0\"", "1/2\"=1'-0\"", "3/4\"=1'-0\"", "1\"=1'-0\"", "1-1/2\"=1'-0\"", "1\"=0'-6\"", "1:1"]
scale = rs.ListBox(items, "Select scale", "Scale detail")
if not scale:
return
if scale == "1/16\"=1'-0\"":
intScale = 120
elif scale == "1/8\"=1'-0\"":
intScale = 96
elif scale == "3/16\"=1'-0\"":
intScale = 64
elif scale == "1/4\"=1'-0\"":
intScale = 48
elif scale == "3/8\"=1'-0\"":
intScale = 32
elif scale == "1/2\"=1'-0\"":
intScale = 24
elif scale == "3/4\"=1'-0\"":
intScale = 16
elif scale == "1\"=1'-0\"":
intScale = 12
elif scale == "1-1/2\"=1'-0\"":
intScale = 8
elif scale == "1\"=0'-6\"":
intScale = 6
elif scale == "1:1":
intScale = 1
detail.DetailGeometry.SetScale(intScale, sc.doc.ModelUnitSystem, 1, sc.doc.PageUnitSystem)
#lock detail
items = ["YES","NO"]
lock = rs.ListBox(items,"Lock Detail/s","YES","NO")
if lock == "YES":
detail.DetailGeometry.IsProjectionLocked = True
elif lock == "NO":
detail.DetailGeometry.IsProjectionLocked = False
detail.CommitChanges()
sc.doc.Views.Redraw()
# Set Layout scale
set_namedtexts_on_active_layout('detailscaletext', scale)
if __name__ == '__main__':
setDetailScale()
Can you get this to work al your end?
I realize my code is not straightforward to comprehend so let me know if you want me to elaborate.
-Willem