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

TUTORIAL - Sketch Mapping

Discussion in 'Alibre Script' started by ajayre, Jun 8, 2019.

  1. ajayre

    ajayre Alibre Super User

    This tutorial demonstrates the new Alibre Script sketch mapping feature in Alibre Design 2019. The primary use is when writing scripts that modify existing parts making it easier to create 2D sketches without having to get into complex calculations. A description of the functionality can also be found in the Alibre Script manual.

    We will start off with a part that was created elsewhere, e.g. manually, provided to us, etc. For this example we use a hexagonal extrusion that gets larger towards the top. Diameter of bottom hexagon is 30mm, diameter of top hexagon is 45mm and the height is 70mm.

    InitialPart.png

    Our script will create a notch in the top. The script lets the user choose the face to notch and which edge is the top. If we manually create a 2D sketch on the face we can see the problem.

    FaceIn2DSpace.png

    I have drawn the X and Y axes on top of the face using reference lines. We don't know in advance exactly how the face will be positioned in the 2D space. As it happens one edge is parallel to the X axis which means that the top edge is 86.48 degrees from vertical.

    In order to create a rectangle in the sketch for notch we now have to use trigonometry to calculate the four corners. Here is what the final sketch should look like (drawn manually)

    DesiredResult.png

    Namely a rectangle 10mm x 2mm centered on the top edge. Who wants to do a load of calculations just to draw a rectangle? Nobody. This is where sketch mapping comes in.
     
    NateLiqGrav likes this.
  2. ajayre

    ajayre Alibre Super User

    By using sketch mapping the face is rotated so that it is more convenient to draw on. Once drawing is completed the face is rotated back along with the sketch.

    In our case we will want to rotate the face so that the top edge is on the X axis and the face is above the X axis. Like this:

    MappedFace.png

    Again the X and Y axes are shown as reference lines. The origin (0, 0) is therefore at the bottom left. Now drawing the rectangle becomes easy because all the edges are vertical and horizontal. No complex calculations required. We just need the midpoint of the edge.

    MappedFaceWithSketch.png

    Now let's write our script.
     
    NateLiqGrav likes this.
  3. ajayre

    ajayre Alibre Super User

    First we get the current part

    Code:
    Prt = CurrentPart()
    The we get the face and top edge. Here we have fixed the face and edge but in a utility script it would be better to prompt the user to select them instead.

    Code:
    NotchFace = Prt.GetFace('Face<4>')
    TopEdge = Prt.GetEdge('Edge<8>')
    We create a sketch for notch on the face

    Code:
    NotchSk = Prt.AddSketch('Notch', NotchFace)
    Now the magic. We map the sketch using the top edge so the top edge is now on the X-axis with the first vertex (TopEdge.Vertices[0]) at (0, 0). The face will be above the X-axis.

    Code:
    NotchSk.StartFaceMapping(TopEdge.Vertices[0], TopEdge.Vertices[1])
    We get the mid point of the top edge

    Code:
    MidPoint = TopEdge.Length / 2.0;
    Now we draw a 10mm x 2mm rectangle centered on the mid point. This is drawing on the rotated face making it easy for us to do.

    Code:
    NotchSk.AddRectangle(MidPoint - 5.0, 0.0, MidPoint + 5.0, 2.0, False)
    We are finished with face mapping so we stop it.

    Code:
    NotchSk.StopFaceMapping()
    And here is the result of running the script:

    ScriptResult.png

    The final step is to perform an extrude cut:

    Code:
    Prt.AddExtrudeCut('Notch', NotchSk, 3.0, True)
    Giving:

    Notch.png

    As you can see sketch mapping makes it easier to create 2D sketches on faces without knowing in advance how the face is positioned in 2D space and without needing to perform lots of tedious calculations for the sketch figures.

    Here is the complete script:

    Code:
    # get the current part
    Prt = CurrentPart()
    
    # get the face and top edge 
    NotchFace = Prt.GetFace('Face<4>')
    TopEdge = Prt.GetEdge('Edge<8>')
    
    # create sketch for notch
    NotchSk = Prt.AddSketch('Notch', NotchFace)
    
    # map sketch using top edge so top edge is now on the X-axis with TopEdge.Vertices[0] at (0, 0)
    NotchSk.StartFaceMapping(TopEdge.Vertices[0], TopEdge.Vertices[1])
    
    # draw 10 x 2 rectangle
    MidPoint = TopEdge.Length / 2.0;
    NotchSk.AddRectangle(MidPoint - 5.0, 0.0, MidPoint + 5.0, 2.0, False)
    
    # done with sketch mapping
    NotchSk.StopFaceMapping()
    
    # create the notch
    Prt.AddExtrudeCut('Notch', NotchSk, 3.0, True)
    Andy
     
    Last edited: Jun 8, 2019
    idslk and NateLiqGrav like this.
  4. NateLiqGrav

    NateLiqGrav Alibre Super User

    This will be very beneficial to the community. Thank you!
     
  5. DavidJ

    DavidJ Alibre Super User Staff Member

    I don't follow how the line with 'startFaceMapping' fits with your description. Is the X-axis just what is hard coded? , I don't see where this is specified in the command. Is X axis in this context simply 'horizontal in the sketch' not necessarily the X axis in 3D space? Could you expand a bit on exactly what the command is doing and what the input is?

    May just be me - I find a lot of example scripts suddenly make a non-obvious leap. Examples are supposed to help us learn, so probably benefit from a level of commenting that would be excessive in normal use.
     
  6. ajayre

    ajayre Alibre Super User

    StartFaceMapping always aligns the chosen edge with the X-Axis. This behavior is hard-coded.

    There is no 3D space in this context - 2D sketches are 2D only and they have their own 2D coordinate system. When moving the pointer around while editing a 2D sketch you can see the X and Y coordinates in the status bar at the bottom right of the window.

    The X-axis is always horizontal - that is a convention always used in 2D mathematics - passing through the origin.

    One good way of understanding examples is to try them out and then start tweaking them and seeing what happens. If you are not sure how they work then please ask on this forum so I can see where the gaps are and fill them.

    Andy
     
    Last edited: Jun 8, 2019
  7. idslk

    idslk Alibre Super User

    Hello Andy,
    as far as a script won't move the cursor around while the script is looking at the statusbar...;)
    Here the question: Is there a "rule" for x and y direction?
    Regards
    Stefan
     
  8. DavidJ

    DavidJ Alibre Super User Staff Member

    Andy - I said that I wasn't clear about one line of code. I do understand basic maths, and 2D vs 3D.

    So what I now assume is that the function maps the line joining the nominated vertices, onto the X-axis of the sketch. Presumably with first vertex at the origin.

    [EDIT - yes looking back in the thread that seem to fit]
     
    Last edited: Jun 8, 2019
  9. NateLiqGrav

    NateLiqGrav Alibre Super User

    It creates a temporary XY coordinate system mapping of the face aligning the temporary X axis with the specified vertex of the edge.
     
    ajayre likes this.
  10. idslk

    idslk Alibre Super User

    Means the temporary plus x-axis direction is from TopEdge.Vertices[0] to TopEdge.Vertices[1] and y-axis is on the side of the choosen face?
    like this:
    upload_2019-6-8_19-23-44.png
    Regards
    Stefan
     
  11. NateLiqGrav

    NateLiqGrav Alibre Super User

    I think the explanation in the Quick Start is really good.
     
  12. idslk

    idslk Alibre Super User

    Hello Nate,

    maybe...but i'm not running the beta;) and this thread is in standard AlibreScript...

    Regards
    Stefan
     
  13. NateLiqGrav

    NateLiqGrav Alibre Super User

     
  14. idslk

    idslk Alibre Super User

    Hello Nate,
    i'm sorry, but as well as i can read, it doesn't answers my question...
    This "teaser" is posted in the normal script forum, so also for non beta testers.
    Nevertheless maybe you or Andy could give me a short answer...
    At least i'm happy about such a function and i'm waiting for the release of 2019
    Regards
    Stefan
     
  15. ajayre

    ajayre Alibre Super User

    Yes.

    Andy
     
  16. idslk

    idslk Alibre Super User

    Thank you Andy,

    Perfect!:)

    Regards
    Stefan
     
  17. NateLiqGrav

    NateLiqGrav Alibre Super User

    Stefan,
    Sorry, I did not see the question mark. I thought you were explaining not asking.
     

Share This Page