Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

findBRepUsingRay returns unexpected result

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
karolis.s
91 Views, 2 Replies

findBRepUsingRay returns unexpected result

Hello,

I am currently working on an add-in for Autodesk Fusion 360, which involves reading and assigning names to faces on a currently active body. The primary objective is to determine the orientation of each face on the body, specifically identifying which faces are top, bottom, left, right, front, and back. This is achieved through a combination of methods, including finding minimum and maximum XYZ coordinates.

Here is a 3D model, including mitered angles, to illustrate the issue:
https://a360.co/3ZfcMxZ

The main challenge I am facing is related to the 'findBRepUsingRay' method, which, in some cases, returns more than zero intersections on mitered angle faces, despite there appearing to be no possible intersections from that particular point and direction. It's essential to note that all other bodies are hidden, and the ray is drawn from the centroid point of the face in question.

karoliss_0-1695036956615.png

Below is a snippet of my code for reference:

 

 

#Author-
#Description-

import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        # ui.messageBox('Hello script')

        cam: adsk.cam.CAM = app.activeDocument.products.itemByProductType('CAMProductType')
        camOcc = cam.designRootOccurrence
        root_c = camOcc.component
        body :adsk.fusion.BRepBody = camOcc.bRepBodies[0]
        faces :adsk.fusion.BRepFaces = body.faces

        valid_faces = []
        intersection_faces = []

        for face in faces:
            intersections = root_c.findBRepUsingRay(
            face.centroid, get_normal(face), adsk.fusion.BRepEntityTypes.BRepFaceEntityType)
            if intersections.count == 0:
                valid_faces.append(face)
            else: 
                intersection_faces.append(face)


        for item in valid_faces:
            draw_normal(item, 'None Intersections')
             
        for item in intersection_faces:
            draw_normal(item, 'Intersections')
            
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def get_normal(face :adsk.fusion.BRepFace):
    evaluator = face.evaluator
    point_3d = adsk.core.Point3D.create(face.centroid.x, face.centroid.y, face.centroid.z)
    _, face_normal = evaluator.getNormalAtPoint(point_3d)
    return face_normal

def draw_normal(face :adsk.fusion.BRepFace,name):
    doc = Doc()
    normal = get_normal(face)
    centroid_point: adsk.core.Point3D = face.centroid.copy()
    normal.normalize()
    target_point: adsk.core.Point3D = centroid_point.copy()
    target_point.translateBy(normal)
    sketch: adsk.fusion.Sketch = doc.root_c.sketches.add(doc.root_c.xYConstructionPlane)
    sketch.name = name
    sketch.sketchCurves.sketchLines.addByTwoPoints(centroid_point, target_point)


import adsk, adsk.core, adsk.cam, adsk.fusion

class Doc:
    def __init__(self):
        app = adsk.core.Application.get()
        ui  = app.userInterface
        productType = app.activeProduct.productType

        # root component
        self.root_c :adsk.fusion.Component = None
        self.body: adsk.fusion.BRepBody = None

        self.data_file = self.__get_active_doc_data_file()

        if productType == 'CAMProductType':
            cam: adsk.cam.CAM = app.activeDocument.products.itemByProductType('CAMProductType')
            camOcc = cam.designRootOccurrence
            self.root_c = camOcc.component

            camOcc.component

            self.body = camOcc.bRepBodies[0]
            
            self.cam: adsk.cam.CAM = cam

            # for setup in self.cam.setups:
            #     s :adsk.cam.Setup = setup
                # s.deleteMe()

        elif productType == 'DesignProductType':
            design = app.activeProduct
            self.root_c = design.rootComponent
            self.body = self.root_c.bRepBodies.item(0)

        if self.root_c == None or self.body == None:
            ui.messageBox('Active product is not Design or CAM product')
            return    
        

    def __get_active_doc_data_file(self) -> adsk.core.DataFile:
        try:
            app: adsk.core.Application = adsk.core.Application.get()
            doc: adsk.core.Document = app.activeDocument
            return doc.dataFile
        except:
            return None

 

 

I would greatly appreciate any insights or suggestions from the community regarding this issue. Your expertise and guidance would be invaluable in refining this add-in to function seamlessly with complex 3D models.

Thank you in advance for your assistance.

Best regards, Karolis Šarskus

 

 

 

2 REPLIES 2
Message 2 of 3
kandennti
in reply to: karolis.s

Hi @karolis.s .

 

It seems that the result returned by findBRepUsingRay sometimes includes the surface from which the center of gravity was obtained.

Therefore, I think you will get the desired result if you judge by the number of faces excluding the faces from which the center of gravity was obtained.

・・・
        for face in faces:
            intersections = root_c.findBRepUsingRay(
            face.centroid, get_normal(face), adsk.fusion.BRepEntityTypes.BRepFaceEntityType)
            intersections = [f for f in intersections if face.tempId != f.tempId] #here
            # intersections = [f for f in intersections if face.entityToken != f.entityToken]

            if len(intersections) < 1:
                valid_faces.append(face)
            else:
                intersection_faces.append(face)
・・・
Message 3 of 3
karolis.s
in reply to: kandennti

I think you are right about this method returning the same face, but for some reason I cannot make a check with entityToken and tempId, but I guess centroid comparison will do the job.

karoliss_0-1695050798251.png

 



Thank you


Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report