Hello all, I have an elementary problem that I cannot solve. Surely someone on the forum has an answer.
I need to find the coordinates of the intersection points of a construction plane with a line and/or a spline but the code I wrote presents an error at line 42. In that line I try to find the intersection of the simple line with the plane.
I almost sure that this is not the right method !
Someone can help me ?
Thank you
import adsk.core, adsk.fusion, traceback def run(context): ui = None try: app = adsk.core.Application.get() ui = app.userInterface doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType) design = app.activeProduct # Get the root component of the active design. rootComp = design.rootComponent # Create a new sketch on the xy plane. sketches = rootComp.sketches; xyPlane = rootComp.xYConstructionPlane sketch = sketches.add(xyPlane) # lines collection lines = sketch.sketchCurves.sketchLines origin = adsk.core.Point3D.create(0, 0, 0) line = lines.addByTwoPoints( origin, adsk.core.Point3D.create(0, 10, 0)) # create construction plane planes = rootComp.constructionPlanes # Create construction plane input planeInput = planes.createInput() offset = adsk.core.ValueInput.createByReal(0.5) planeInput.setByDistanceOnPath(line, offset) planes.add(planeInput) plane = planes.item(0) # find the intersection between the line and the plane (result, intersectionResults, intersectionPts) = line.intersections(planes) intersectionPts = plane.intersectWithCurve(line) x = intersectionPts.x y = intersectionPts.y z = intersectionPts.z ui.messageBox("X = "+str(x)+ "Y = " + str(y) + "Z = "+str(z)) except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Solved! Go to Solution.
Solved by ekinsb. Go to Solution.
Here's a slight variation of your program that works. The important thing to understand is that there are "entities" and "geometry". Entity is a term used for things that you see represented in Fusion and can typically be created and selected through the user-interface. For example, a sketch line and a construction plane are both entities. Geometry is a term used for the raw definition of different kinds of shapes. For example the raw geometry definition of a point consists of the x, y, and z coordinates. A line is defined by two points. Geometry definitions can exist without have a visible entity in Fusion.
You can get a geometry object from most Fusion entities. In the code below I call the geometry property of the ConstructionPlane to get the plane geometry definition. A sketch has it's own coordinate system and sketch entities are defined within that coordinate system. In the code below I call the worldGeometry property of the SketchLine object to get a Line3D geometry object that's defined in model space instead of sketch space. Because the geometry objects don't have to be associated with real entities they are much faster to create and are very useful in cases where you don't need the entities or are using them for intermediate calculations.
import adsk.core, adsk.fusion, traceback def run(context): ui = None try: app = adsk.core.Application.get() ui = app.userInterface doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType) design = adsk.fusion.Design.cast(app.activeProduct) # Get the root component of the active design. rootComp = design.rootComponent # Create a new sketch on the xy plane. sketches = rootComp.sketches; xyPlane = rootComp.xYConstructionPlane sketch = sketches.add(xyPlane) # lines collection lines = sketch.sketchCurves.sketchLines origin = adsk.core.Point3D.create(0, 0, 0) line = lines.addByTwoPoints(origin, adsk.core.Point3D.create(0, 10, 0)) # create construction plane planes = rootComp.constructionPlanes planeInput = planes.createInput() offset = adsk.core.ValueInput.createByReal(0.5) planeInput.setByDistanceOnPath(line, offset) constPlane = planes.add(planeInput) # find the intersection between the line and the plane #(result, intersectionResults, intersectionPts) = line.worldGeometry.intersectWithSurface(constPlane.geometry) intersectionPts = constPlane.geometry.intersectWithCurve(line.worldGeometry) x = intersectionPts.item(0).x y = intersectionPts.item(0).y z = intersectionPts.item(0).z ui.messageBox("X = "+str(x)+ " Y = " + str(y) + " Z = "+str(z)) except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Hi Brian, I want you thank a lot for your rapid reply. I’m writing a day after, and I hope you’ll excuse me.
Your answer is complete and it let me understand something more about the Fusion API. I understand your answer even if I failed to reach a solution by myself. At the end your solution is seems very simple and I am almost ashamed for my question.
The sensation is that the APIs are a complex world that is very difficult to penetrate for me !
I spent a lot of time trying to find a solution before disturb you and the forum. For this I would like to ask you if there is some documentation more detailed than the ‘Learn from the experts’ of the Fusion site. Some book or article by which I can better understand the inner mistery of Fusion APIs.
Thank you very much for all your time.
Dino
Hi Dino,
I'm not aware of any other documentation. I'll be the first to admit that we could always use more and better documentation. Are there specific things that you wish had been covered or presented in a better way?
Hi Brian, I thank you very much for your reply. I’m writing few days after your answer because I thought a lot to your reply. It is quite difficult for me to reassume my experience with Fusion. Maybe my difficulties in learning the Fusion Apis and write some script from scratch depends on the fact that it is the first time I approach to the 3D modelling.
Just because you ask me, I will try to express my experience.
Even if I read the ‘Learn from the expert’ documentation and watch your Webinar on API scripting my approach has been always of the kind ‘cut and copy’ from the examples I found on the forum and on the other resource that you mention in the API site (GitHub and Mod the Machine Blog). There is few material other than this on the net !
I perfectly know that this is not the right approach! I should understand what are the basic concepts that are behind the API. Even if I wrote some scripts and add-in (with your help and the forum guys) that has been published from Autodesk blog, there is something I feel that is lacking me.
What I think that is lacking is the connection between the concepts and the application on the scripting. Even if there are a certain number of examples, it’s seem to me that the examples are for people that already know the concepts.
I want to give just an example of my misunderstanding: what are the difference between the objects used in adsk.core and in adsk.fusion. I haven’t found no explanation about it in the documentation.
This is only an example, a basic one, but I could continue with a lot of other, that let me spend a lot of time in searching a solution to a specific problem.
I want to clarify that this is not a polemical post. The goal is to help newbie (like me) to better understand the use of the APIs.
I want to thank a lot for your patience, excuse me for my English. I hope that this post can be useful for someone.
With deep gratitude, Dino