import time # only used for the example at the bottom
def TextChange(SourceSketch, NewText = 'NewText', ThisID = 0, verbose = 0):
# Uses the AlibreX API be very careful of modification
NS = SourceSketch._Sketch
try:
NS.EndChange()
NS.BeginChange()
except:
pass
textIDs = []
for figindex in range(0, NS.Figures.Count):
try:
fig = NS.Figures.Item(figindex)
if fig and str(fig.FigureType) == 'AD_SKETCHTEXT':
textIDs.append(fig.ID)
except:
print('Could Not Find Any Text')
if verbose:
print('Availible Text IDs = ' + str(textIDs))
if ThisID > 0:
if str(ThisID) in textIDs:
fig2 = NS.Figures.GetFigureByID(str(ThisID))
if str(fig2.FigureType) == 'AD_SKETCHTEXT':
if verbose:
print('This Text ID = ' + str(fig2.ID))
print('Original Text = ' + str(fig2.TextString))
fig2.TextString = str(NewText)
fig3 = NS.Figures.GetFigureByID(str(ThisID))
if verbose:
print('Updated Text = ' + str(fig3.TextString))
else:
print('This ID = ' + str(ThisID) + ' Is not text')
else:
print('Could Not Find Text With ID = ' + str(ThisID))
elif any(textIDs):
for x in textIDs:
fig4 = NS.Figures.GetFigureByID(str(x))
if str(fig4.FigureType) == 'AD_SKETCHTEXT':
if verbose:
print('This Text ID = ' + str(fig4.ID))
print('Original Text = ' + str(fig4.TextString))
fig4.TextString = str(NewText)
if verbose:
fig5 = NS.Figures.GetFigureByID(str(x))
print('Updated Text = ' + str(fig5.TextString))
else:
print('This ID = ' + str(x) + ' Is not text')
else:
print('Could Not Find Any Text')
try:
NS.EndChange()
except:
pass
return textIDs
# the following is all only for example usage
MyPart = CurrentPart()
MySketch = MyPart.GetSketch('Sketch<1>')
TextChange(MySketch,'New Text',0,1) # Will change all text in the sketch and also print out information as it does it.
time.sleep(2)
TextChange(MySketch,'Hello\r\nWorld') # Make sure you use carriage return and newline for multiline text.
time.sleep(2)
TextChange(MySketch,'One',1) # Will change text with that figure ID in the sketch.
time.sleep(2)
TextChange(MySketch,'Two',2) # Will change text with that figure ID in the sketch.
time.sleep(2)
TextChange(MySketch,'Three',3) # Will change text with that figure ID in the sketch.
time.sleep(2)
TextChange(MySketch,'This Fails',5) # Will try and fail to find that figure ID in the sketch.