1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Iterating through part sketches

Discussion in 'Alibre Script' started by technolazy, Jan 26, 2020.

  1. technolazy

    technolazy Member

    I'm probably missing something, but is the Part.Sketches object actually implemented? It's not documented in the Alibre Script reference, but calling part.Sketches does not raise any exception. However, the array always has 0 elements, despite the part having many sketches (with but also without features based upon them). Or is that just an Interface?
    I'm looking for a method to iterate through all sketches, whose name is not known in advance. Thx.
     
  2. idslk

    idslk Alibre Super User

    Sketches is a collection. Take a look into the API Help.
     
  3. technolazy

    technolazy Member

    I did read the API and before posting here I also went through all the examples looking for a similar use case, but haven't found any.
    This is what I'm doing:

    prt = CurrentPart()
    sketches = prt.Sketches
    print sketches.Count # ==> this prints 0
    print "names:"
    for sketch in sketches:
    print sketch.Name # ==> (obviously) never arrives here

    There is this sentence in the Alibre API help (also not the Alibre Script help) (help page for IADSketches.Count Property):
    The count property for the Sketches object held by automation clients will not get updated automatically when a sketch is added or deleted. Get the current Sketches collection by querying the Part Session.
    Does this explain that? But then is there a way to get the "Part Session" from Alibre Script? is the object returned by CurrentPart() not a Part Session?
     
  4. axeme

    axeme New Member

    I went through the same thing at one point wanting to use AlibreScript to rename sketches as the API allows via the IADSketch::Name Property, but AlibreScript doesn't have this capability.

    Maybe this can get you started:
    Code:
    my_part = CurrentPart()._Part
    part_features = my_part.Features.GetEnumerator()
    
    for f in part_features:
        print(f.Sketch.Name)
        # list the module's attributes
        print(dir(f.Sketch))
     
    
    I don't think this is documented anywhere
     
    NateLiqGrav and technolazy like this.
  5. technolazy

    technolazy Member

    That definitely got me started, thanks!
    So the key is using the _Part property and the GetEnumerator() method.
    In order to get the list of all part sketches (regardless if they already have a feature associated with them), this is what now works for me:
    Code:
    for s in CurrentPart()._Part.Sketches.GetEnumerator():
      print s.Name
    
    _Part apparently gives access to the "Part session" mentioned in the Alibre API help, but one has to be aware that the "Part session" is not the same thing as the AlibreScript Part object. In particular, they do not share the same methods. So for instance, you cannot call AddSketch() on a "Part session" object, as you would with an AlibreScript Part like the one returned by CurrentPart(). In my case I needed to use both objects. Below is the working solution I came up with. The code iterates through all sketches and creates a copy of each one.
    Code:
    my_part = CurrentPart() # object of type AlibreScript.API.Part
    my_part_session = my_part._Part # object of type AlibrePartSession
    
    # get the enumerator, which is required to loop through the elements of the collection
    part_sketches = my_part_session.Sketches.GetEnumerator()
    
    for s in part_sketches:
      print(s.Name)
      # create a new empty sketch. For determining the name and the plane, we use the my_part object (instance of AlibreScript.API.Part)
      copied_sketch = my_part.AddSketch('copy_of_'+str(s.Name), my_part.GetPlane('XY-Plane'))
      # we cannot use the sketch object from my_part_session.Sketches as an argument for CopyFrom(), that would raise an error.
      # instead, we access the sketch object using my_part.GetSketch (AlibreScript.API.Part) passing the sketch name (from the collection derived from the session object) as an argument
      copied_sketch.CopyFrom(my_part.GetSketch(s.Name))
    
    Hope this helps.
     
    axeme likes this.
  6. idslk

    idslk Alibre Super User

    Hello @technolazy ,
    there are a lot of ways to rome...
    here another one including sketch rename for @axeme ...
    Code:
    # Print names
    for x in range(CurrentPart()._Part.Sketches.Count):
      print 'Sketch Nr. ',x,' Name: ',CurrentPart()._Part.Sketches.Item(x).Name
    # Rename Sketch 3 to Testrename
    x = 2  # Attention! The collection has to have a least 3 items,otherwise change the value of x ;-)
    CurrentPart()._Part.Sketches.Item(x).Name = 'Rename_Test'
    print 'Going to rename Sketch Nr. ',x
    print 'Sketch Nr. ',x,' Name: ',CurrentPart()._Part.Sketches.Item(2).Name
    One question: For what you need the copysketch? A simple script
    Regards
    Stefan
     
    axeme likes this.
  7. technolazy

    technolazy Member

    Hi Stefan,
    You are right, all roads will bring you to Rome, and I would add: once one knows where the keys of the car are... in this case, CurrentPart._Part is the key.
    The script excerpt I posted was simplified for illustrative purposes; I'm actually planning to use the more complex version of CopyFrom with scale factor. I want to be able to generate several smaller scale versions from the same sketch, in situations where scaling the whole part is not optimal, and I want to be able to replicate the process ad libitum (hence the need to script it). But I can imagine a lot of situations where being able to iterate through all sketches would really come handy. Thx all for the help.
     
  8. idslk

    idslk Alibre Super User

    Hello @technolazy,
    have you taken a look at the skript/link i have posted? It has a scale factor, plane creation, rotation angle...
    Regards
    Stefan
     
  9. technolazy

    technolazy Member

    Hi @idslk, I did, thanks, great tool. I rather need to work with script directly at the moment, but will keep that in mind. Best
     

Share This Page