Projecting a face to a sketch only split half

Projecting a face to a sketch only split half

alex_winter
Advocate Advocate
4,833 Views
14 Replies
Message 1 of 15

Projecting a face to a sketch only split half

alex_winter
Advocate
Advocate

Hello, I Need some help trying to project a face onto a sketch that has been split using the split body feature. When I run the script it projects around the whole component and not the body/face.

So the way this is suppose to work is Make a copy of body1 and then split that copied body in half using splitbody and then make a sketch on that split-body xZ plane and then project the split face(Z+ side only) onto the sketch.

The Part I am testing this on is a tube with a Through Hole in the center of it. I have attached it below.

 

 

 

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        

        # Get active design
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        if not design:
            ui.messageBox('No active Fusion design', 'No Design')
            return

        # Get root component in this design
        rootComp = design.rootComponent

        # Get the sub component
        occs = rootComp.occurrences
        compA = occs.item(6).component

        # Get the body in the sub component  
        body1 = compA.bRepBodies[0]
        
        # Copy/paste body from sub component to sub component
        compA.features.copyPasteBodies.add(body1)

        # Get the second copied body  
        body2 = compA.bRepBodies[1]

        # Create SplitBodyFeatureInput
        splitBodyFeats = rootComp.features.splitBodyFeatures
        splitBodyInput = splitBodyFeats.createInput(body2, compA.xZConstructionPlane, True)

        # Create split body feature
        splitBodyFeats.add(splitBodyInput)

        
        # Get the second body split  
        body3 = compA.bRepBodies[1]

        targetBody: adsk.fusion.BRepBody = body3

        skt: adsk.fusion.Sketch = compA.sketches.add(
            compA.xZConstructionPlane
        )

        skt.project(targetBody)
        
            
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

 

0 Likes
Accepted solutions (2)
4,834 Views
14 Replies
Replies (14)
Message 2 of 15

Jorge_Jaramillo
Collaborator
Collaborator

Hi @alex_winter ,

 

You need to project the faces that are in the XZ plane, instead of the whole body (or project all of the faces and choose the profiles you want, since I don't have your design to test it here).

 

Replace the last sentence in your function by this:

    # skt.project(body3)
    for face in body3.faces:
        (ret, normal) = face.evaluator.getNormalAtPoint(face.centroid)
        if ret and normal.x == 0 and normal.z == 0:
            skt.project(face)

 

Let me know if it works for you.  Hope this help.

 

Regards,

Jorge

0 Likes
Message 3 of 15

alex_winter
Advocate
Advocate

Thanks for replying.  I have tested your code out and it works except It is doing it on both sides of the plane.  I will show you a picture.

0 Likes
Message 4 of 15

Jorge_Jaramillo
Collaborator
Collaborator

I got the same result as you, since the XZ plane cuts the tube generating two planar faces.

Do you expect only one of them?  If so, the slice need to be done with a plane that only cuts the body just in one part of the tube.

Or am I missing something?

 

Regards,

Jorge

0 Likes
Message 5 of 15

alex_winter
Advocate
Advocate
Yes I was expecting only one side for the sketch to be on preferably the Z+ Side.
0 Likes
Message 6 of 15

Jorge_Jaramillo
Collaborator
Collaborator

I'm lost.
What do you mean by "the sketch to be on preferably the Z+ side"?

Is you Y+ side point up in you screen?
I use the split plane you had in the initial script, which is on plane XZ.

0 Likes
Message 7 of 15

alex_winter
Advocate
Advocate

Sorry yes in the component Origin, I have Y+ is up just like in the last picture I sent.

so when I mean ""the sketch to be on preferably the Z+ side" that is the right side in the picture above.  What you have is correct except I just want the one side in that picture (right side).

0 Likes
Message 8 of 15

Jorge_Jaramillo
Collaborator
Collaborator
Do you mean XY plane?
0 Likes
Message 9 of 15

alex_winter
Advocate
Advocate

No XZ Plane. It is splitting on the correct plane but I only want the one side not both.  Here is another picture.  I also have the fusion file in the original post if it helps.

0 Likes
Message 10 of 15

Jorge_Jaramillo
Collaborator
Collaborator
Accepted solution

You just need to select the face which centroid has a positive Z coordinate, like so:

    for face in body3.faces:
        (ret, normal) = face.evaluator.getNormalAtPoint(face.centroid)
        app.log(f'{ret=} {normal.asArray()=}')
        if ret and normal.x == 0 and normal.z == 0 and face.centroid.z > 0:
            skt.project(face)

 

It worked for me (my model orientation is Z+ up):

wtallerdemadera_0-1660333686538.png

Just one profile projected.

 

Regards,

Jorge

 

0 Likes
Message 11 of 15

alex_winter
Advocate
Advocate
Perfect 🙂 Thanks that worked.
0 Likes
Message 12 of 15

alex_winter
Advocate
Advocate

I have made an adjustment which I am trying to get working properly in the above code.  So instead of a designated plane(XZ), I made an adjustment to now have the user select the plane to split on.  After I get the second body in the code above, I added this: 

msg :str = 'Select ConstructionPlanes'
        selFiltter :str = 'ConstructionPlanes'
        sel :adsk.core.Selection = selectEnt(msg ,selFiltter)
        if not sel: return

        # selected plane
        basePlane :adsk.fusion.ConstructionPlane = sel.entity

 and then replaced compA.xZConstructionPlane with basePlane in splitBodyFeats.createInput line.  This all works except for I cant get skt.project(body3) to create a project sketch.  I  sort of understood how face.evaluator worked before but now we cant constrain it on the normal.x and normal.z since we dont know what axis is selected.

 

0 Likes
Message 13 of 15

Jorge_Jaramillo
Collaborator
Collaborator
Accepted solution

Hi @alex_winter ,

 

You wont get any projection because this condition restricts the type of faces to project:

 

 

. . .
if ret and normal.x == 0 and normal.z == 0 and face.centroid.z > 0:
. . .

 

 

Since you are changing the plane tool to split the body, you might also need to change the orientation of the sketch to project to and project on it only the faces that are parallel to it, like so:

 

 

 

skt: adsk.fusion.Sketch = compA.sketches.add(basePlane) # define sketck plane parallel to the split plane tool
# skt: adsk.fusion.Sketch = compA.sketches.add(compA.xZConstructionPlane)

for face in body3.faces:
    (ret, normal) = face.evaluator.getNormalAtPoint(face.centroid)
    face_plane = adsk.core.Plane.create(face.centroid, normal)
    if face_plane.isCoPlanarTo(basePlane.geometry): # project only parallel faces to split plane tool
        skt.project(face)

 

 

 

It worked on my side with all 3 main planes of the design.

It is what you need?

 

Regards,

Jorge

 

 

Message 14 of 15

alex_winter
Advocate
Advocate
Thanks :), this worked on that model I gave you (since the component origin and rootComp origin are in the same location) but I have another where the component origin is at a different location and that one didn't make the project sketch.
0 Likes
Message 15 of 15

Jorge_Jaramillo
Collaborator
Collaborator

Hi @alex_winter ,

 

If I understood you well, you have to change the sketch plane.

In the example I sent to you I used the same plane for both, split and sketch.

If you need to sketch on a different plane, you can add a new call to "selectEnt()" to let the user choose the sketch plane to project to.

 

Hope this help.

 

Regards,
Jorge

0 Likes