What's new

Sketch Text not available in API?

jordanyte

Member
I am trying to finish up a script. Part of the script is to update a text marking on an electronic component. The text marking is made by an extrusion from a sketch which has a Sketch Text object.

However, I cannot find any reference to Sketch Text objects in the API documentation. Is this a new feature type that is not yet available in the API or is the documentation not updated yet? Could someone in Alibre development please tell me how I might be able to edit the string in the text object via Alibre Script?

Many thanks!
-Ben.
 

NateLiquidGravity

Alibre Super User
Sketch text figures are not supported at all in the AlibreScript API, but reading and modifying the text figures is possible by the AlibreX API. We can not create text figures by either API at this time, so they have to be manually added to the sketches first.

I have been working on a function wrapping the AlibreX API functions to change existing text in sketches for a while. I added it below.

The example included in the code assumes:
You have a part open.
You add a sketch named 'Sketch<1>'
The only things you added to the sketch are three different text figures.
You exited the sketch and extruded the sketch some distance so you can see the changes.

Python:
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.
 
Last edited:

jordanyte

Member
Thankyou for this. It will do what I need, but I have a newb question:
Does this mean I am only able to perform this running python from outside Alibre, importing the AlibreX namespace?
Or can this execute from within the AlibreScript environment>if so what import statement do I need to access the AlibreX API within the script?
 

jordanyte

Member
Never mind. I'm not a Python programmer. I was raised on embedded C and C++...

The problem was the indentation was mangled on the first try:/except: clause.

This works and solves my issue perfectly. Thank you so very much!
 

bolsover

Senior Member
Never mind. I'm not a Python programmer. I was raised on embedded C and C++...

The problem was the indentation was mangled on the first try:/except: clause.

This works and solves my issue perfectly. Thank you so very much!

If you are comfortable with C and C++, you might find working with an AddOn and C# more to your liking.
I tried getting to grips with the scripting environment but found it restrictive and I don't really like Python, preferring the structure and power of more formal languages.
David
 

jordanyte

Member
I explored C# with this when it was Geomagic Design and they first released the API. I need to go back and look into it again. Yes, C# is a far nicer language than Python... (subjective but still... )
 
Top