What's new

figures

Rob2beans

Member
is there a way to remove figures?

I am new to scripting and have limited knowledge in python programming.

I wrote a script to subdivide a bspline and place points along the divides.
I would like to delete the bspline after the points are placed is this possible?
 

NateLiquidGravity

Alibre Super User
You can by interfacing with the AlibreX API side of things - which is complicated. Or perhaps when creating the bspline in Alibre Script make it a reference figure by setting the bool IsReference to 1 for true.
 

R-man

Senior Member
Off the top of my head, I'd suggest opening a temporary part, create the spline there, and save the coordinates of your dividing points for use in your main part. No need to delete the temporary part, just don't save it.
 

Rob2beans

Member
Nate, thanks for the reply!
What I have accomplished at this point is select a bspline from a sketch that could have multiple bsplines within the sketch and then subdivide a specific number according to length. Then I manually connect the points with another bspline then delete the old bspline.
what i would like is to be able to select the bspline by just clicking on it to know graphically what i am selecting especially when multiple splines are in the sketch, is that possible with script?
Then it would be great if it could be deleted after subdividing and placing points on it, don't think it is possible after reading you reply.
The other thing is alibre freezes if i run the code with a non-closed sketch open. is there something i can do within the code to close the open sketch before anything else is executed?

I attached the code, please excuse the mess, it is my first attempt!!


Python:
Win = Windows()
Units.Current = UnitTypes.Inches
MyPart = CurrentPart()
question = True
ScriptName = 'Equal Distant Points On Spline'
t = 0

while question == True:
 # create dialog window and show to user
 Options = []
 Options.append([None, WindowsInputTypes.Image, "MLogo.png", 40])
 Options.append(['Source Sketch', WindowsInputTypes.Sketch, None])
 Values = Win.OptionsDialog("Select Sketch That Contains Spline", Options, 340)
 if Values == None:
   sys.exit()

# get user inputs
 SketchSelect = str(Values[1])

#Acivate Sketch
 MySketch = MyPart.GetSketch(SketchSelect)

#List of Figures in sketch
 Fig = len(MySketch.Figures)

 
#input the spline figure to subdivide
 bsplines = []
 print "\n\n\n\n\n         Begin of Sketch Figure List"

 for index, figure in enumerate(MySketch.Figures):
  if isinstance(figure, Bspline):
    print "Bspline # " + str(index+1) + "---" + str(figure)
    bsplines.append(str(index+1))

 print "         End of Sketch Figure List"

 Options = []
 Options.append(['Enter the figure number of the Bspline to divide', WindowsInputTypes.StringList, bsplines])
 Values = Win.OptionsDialog(ScriptName, Options, 170)
 if Values == None:
  sys.exit()

 FigureNumber = int(bsplines[Values[0]])
 LOS = MySketch.Figures[FigureNumber-1].Length
 SINFO = str(MySketch.Figures[FigureNumber-1])
 Win.InfoDialog("Length "+ str(round(LOS, 2))+" Inches"+ "\n"+ SINFO, "Info for BSpline # "+str(FigureNumber))

 Options.append(['Number of equal distant divides along Bspline', WindowsInputTypes.Integer, None])

 Values = Win.OptionsDialog("Select Figure and Number of Divides Along Bspline", Options, 170)
 if Values == None:
  sys.exit()


 Segments = Values[1]

 if Segments < 2:
  Win.ErrorDialog("Specify at least 2 segments", "ERROR")
  sys.exit()
#Divide Bspline into equal lengths
 SP = MySketch.Figures[FigureNumber-1].Subdivide(Segments)
 SPBegin = MySketch.Figures[FigureNumber-1].GetPointAt(0.0)
 SPEnd = MySketch.Figures[FigureNumber-1].GetPointAt(1)
 WOS = MySketch.Figures[FigureNumber-1].Weights
 OOS = MySketch.Figures[FigureNumber-1].Order
 CPS = MySketch.Figures[FigureNumber-1].ControlPoints
 KVS = MySketch.Figures[FigureNumber-1].KnotVectors

#print "my weights are"
#print WOS
#print "order number"
#print OOS
#print "control points"
#print CPS
#print "knot Vectors"
#print KVS

#Set the range number requires X and Y List to be doubled
 NumberOfDivides = Segments-1
 previous_points = MyPart.GetUserData("mohrcomposites.data.spline_"+str(FigureNumber))
 previous_points = None
 if previous_points is not None:
  
  #I WANTED TO REMOVE THE PREVIOUS POINTS BUT THERE IS NOT FUNCTION ON SKETCH!!!!
  Win.ErrorDialog("You already subdivided bspline " + str(FigureNumber) + ". Segments:" + str(previous_points["segments"]), "ERROR")
 else:
  points=[]
  for i in range(0, NumberOfDivides):
    MySketch.AddPoint(SP[t], (SP[t+1]))
    t = t+2
 t = 0
    
 MySketch.AddPoint(SPBegin[0], SPBegin[1])
 MySketch.AddPoint(SPEnd[0], SPEnd[1])

#  MyPart.SetUserData("mohrcomposites.data.spline_"+str(FigureNumber),{"points":points})     
 MyPart.SetUserData("mohrcomposites.data.spline_"+str(FigureNumber),{"segments":Segments})     
 Win.InfoDialog("Bspline "+str(FigureNumber)+" selected  "+"\nDivided in "+str(Segments)+" sections","COMPLETED")
 question = Win.QuestionDialog("Continue Dividing Splines?", "Finished?")
sys.exit()
 
Last edited:

NateLiquidGravity

Alibre Super User
Are you using Windows 7 by chance? If you are using Windows 7 then a string list will freeze Alibre Script / Alibre Design 100% of the time since a few versions back of Alibre.

While trying to break it I discovered that if you try to run the script while editing the sketch AND the sketch is an open loop it will freeze Alibre Design when the "Status - Sketch Problems Detected" alert pops up. This is something that happens with every script. I plan to report that to support.
 

NateLiquidGravity

Alibre Super User
Here is your script with some modifications. I have Windows 7 - so I changed the stringlist to an integer input. I added the ability to erase/delete the spline figure at the end. I also added some other things to simplify thing for you. Look it over closely.

Python:
### 'Equal Distant Points On Spline'
### https://www.alibre.com/forum/index.php?threads/figures.23994/
Win = Windows()
Units.Current = UnitTypes.Inches
MyPart = CurrentPart()
question = True
ScriptName = 'Equal Distant Points On Spline'
t = 0

while question == True:
    # create dialog window and show to user
    Options = []
    Options.append([None, WindowsInputTypes.Image, "MLogo.png", 40])
    Options.append(['Source Sketch', WindowsInputTypes.Sketch, None])
    Values = Win.OptionsDialog("Select Sketch That Contains Spline", Options, 340)
    if Values == None:
        print('User Canceled')
        sys.exit()
    if not any(Values):
        print('Nothing Selected')
        sys.exit()
    # get user inputs
    MySketch = Values[1]

    #input the spline figure to subdivide
    bsplines = []
    print "\n\n\n\n\n         Begin of Sketch Figure List"

    for index, figure in enumerate(MySketch.Figures):
        if isinstance(figure, Bspline):
            print "Bspline # " + str(index+1) + "---" + str(figure)
            bsplines.append(index+1)

    print "         End of Sketch Figure List"
    if not any(bsplines):
        print('No bsplines in this sketch')
        sys.exit()

    Options = []
    Options.append(['Enter the figure number of the Bspline to divide', WindowsInputTypes.Integer, 1])
    Values = Win.OptionsDialog(ScriptName, Options, 170)
    if Values == None:
        print('User Canceled')
        sys.exit()
    if not any(Values):
        print('Nothing Selected')
        sys.exit()
    if Values[0] not in bsplines:
        print('Not a valid bspline figure')
        sys.exit()

    FigureNumber = Values[0]
    LOS = MySketch.Figures[FigureNumber-1].Length
    SINFO = str(MySketch.Figures[FigureNumber-1])
    InfoString = "Info for BSpline # " + str(FigureNumber) + "\nLength "+ str(round(LOS, 2))+" Inches"+ "\n"+ str(SINFO)
    
    Options = []
    Options.append(['Label', WindowsInputTypes.Label, InfoString])
    Options.append(['Number of equal distant divides along Bspline', WindowsInputTypes.Integer, 2])
    Options.append(['Erase Spline figure when done', WindowsInputTypes.Boolean, 0])
    Values = Win.OptionsDialog("Select Figure and Number of Divides Along Bspline", Options, 170)
    if Values == None:
        print('User Canceled')
        sys.exit()
    if not any(Values):
        print('Nothing Selected')
        sys.exit()

    Segments = Values[1]
    EraseSpline = Values[2]
    
    if Segments < 2:
        Win.ErrorDialog("Specify at least 2 segments", "ERROR")
        sys.exit()
    #Divide Bspline into equal lengths
    SplineFigure = MySketch.Figures[FigureNumber-1]
    SP = SplineFigure.Subdivide(Segments)
    SPBegin = SplineFigure.GetPointAt(0.0)
    SPEnd = SplineFigure.GetPointAt(1)
    WOS = SplineFigure.Weights
    OOS = SplineFigure.Order
    CPS = SplineFigure.ControlPoints
    KVS = SplineFigure.KnotVectors

    #print "my weights are"
    #print WOS
    #print "order number"
    #print OOS
    #print "control points"
    #print CPS
    #print "knot Vectors"
    #print KVS

    #Set the range number requires X and Y List to be doubled
    NumberOfDivides = Segments-1
    previous_points = MyPart.GetUserData("mohrcomposites.data.spline_"+str(FigureNumber))
    previous_points = None
    if previous_points is not None:

        #I WANTED TO REMOVE THE PREVIOUS POINTS BUT THERE IS NOT FUNCTION ON SKETCH!!!!
        Win.ErrorDialog("You already subdivided bspline " + str(FigureNumber) + ". Segments:" + str(previous_points["segments"]), "ERROR")
    else:
        points=[]
        for i in range(0, NumberOfDivides):
            MySketch.AddPoint(SP[t], (SP[t+1]))
            t = t+2
    t = 0

    MySketch.AddPoint(SPBegin[0], SPBegin[1])
    MySketch.AddPoint(SPEnd[0], SPEnd[1])
    
    # If EraseSpline was selected this will use the undocumented
    # Alibre Script / AlibreX API connection to delete the
    # spline figure from the sketch.
    if EraseSpline:
        objSplineFigure = SplineFigure.FigureObject()
        objSketch = objSplineFigure.Sketch
        try:
            objSketch.BeginChange()
        except:
            print "Unexpected error:", sys.exc_info()[0]
            pass
        objSplineFigure.Delete()
        try:
            objSketch.EndChange()
        except:
            print "Unexpected error:", sys.exc_info()[0]
            pass
    
    #  MyPart.SetUserData("mohrcomposites.data.spline_"+str(FigureNumber),{"points":points})     
    MyPart.SetUserData("mohrcomposites.data.spline_"+str(FigureNumber),{"segments":Segments})     
    Win.InfoDialog("Bspline "+str(FigureNumber)+" selected  "+"\nDivided in "+str(Segments)+" sections","COMPLETED")
    question = Win.QuestionDialog("Continue Dividing Splines?", "Finished?")
sys.exit()
 

Rob2beans

Member
Nate!!
Thank you for the mods, this works excellent.
I am going to study this script carefully.

in the case of undocumented AlibreScript is there a way to get the list?

In case your curious about the application of this script.
My business is in aerospace, there are a lot of foam wing cores cut with an in house-built CNC hotwire cutter. When it comes to setting up the airfoils from CAD to be cut they need an equal number of points on the wing tip and wing base airfoil skectch to cut the wing properly. Before i had to manually place all the points using reference lines and measurements and then redraw the spline, so time consuming.
Another good use for the script is making complex sketches loft correctly every time. Kind of the same process as the airfoils.

side of gurney flap.jpg
 

NateLiquidGravity

Alibre Super User
If you're asking if there is a list of the undocumented (and unsanctioned) things you can do in Alibre Script. Simply no. I will say you can access nearly all AlibreX API interfaces directly in Alibre Script's IronPython without resorting to coding c# functions. The AlibreX API is difficult to understand as examples are sparse but it offers much more ability. The hardest part is finding a method of transitioning between the Alibre Script API and the AlibreX API. You can usually find hidden things using dir() or help()
Python:
print(dir(whatever_object))
#or
print(help(whatever_object))
In Python nothing is really hidden but typically an underscore asks you not to use directly.

Further - nearly the entire Microsoft .NET library is accessible after import to use in Alibre Script's IronPython.
 
Top