Copy a sketch from one component into another without changing its position in world space?

Copy a sketch from one component into another without changing its position in world space?

Joshua.mursic
Advocate Advocate
685 Views
5 Replies
Message 1 of 6

Copy a sketch from one component into another without changing its position in world space?

Joshua.mursic
Advocate
Advocate

Hello, is it possible to copy a sketch, or at least recreate it, from its source component into another component with a different transform? In the image below I would like to copy the sketch in component 2 into component 1,without moving the global position of the sketch using the API. 

Joshuamursic_0-1706812920260.png

 

I am not sure where I am going wrong here. Ay help is greatly appreciated. I have attached the test project above to make things easier.

 

 

import adsk.core, adsk.fusion, traceback

def run(context):
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design:adsk.fusion.Design = app.activeDocument.products.itemByProductType("DesignProductType")
        # design.designType = adsk.fusion.DesignTypes.ParametricDesignType
        root = design.rootComponent
        occ1 = root.occurrences.item(0)
        occ2 = root.occurrences.item(1)
        comp1 = occ1.component
        comp2 = occ2.component

        #get the sketch to be copied
        sketch = comp2.sketches.item(0)
        #create the target sketch to copy the sketch into
        newSketch = comp1.sketches.add(comp1.xYConstructionPlane)

        sketchLines = adsk.core.ObjectCollection.createWithArray([line for line in sketch.sketchCurves.sketchLines])
        transform = adsk.core.Matrix3D.create()
        fO,fX,fY,fZ = occ1.transform2.getAsCoordinateSystem()
        tO,tX,tY,tZ = occ2.transform2.getAsCoordinateSystem()
        transform.setToAlignCoordinateSystems(fO,fX,fY,fZ,tO,tX,tY,tZ)
        
        sketch.copy(sketchLines,transform,newSketch)
        
    except:
        ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

 

 

0 Likes
686 Views
5 Replies
Replies (5)
Message 2 of 6

BrianEkins
Mentor
Mentor

I'm not sure I understand. Are you saying that if your program works correctly you'll have two sketches with the same geometry directly on top of each other, but the sketches will be in different components?

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 3 of 6

Joshua.mursic
Advocate
Advocate
Yes, that’s correct
0 Likes
Message 4 of 6

Joshua.mursic
Advocate
Advocate

Yes that is exactly correct 

0 Likes
Message 5 of 6

Joshua.mursic
Advocate
Advocate

The issue is that the sketch.transform does not correctly apply transformations. I have been trying to find work arounds by creating sketches inside of occurrences and then transforming the occurrences, but there is not way to correctly get the new sketch position in the context of the original occurrence. There is another thread that I have started that essential has the same issue. An you said that there is probably a bug with the sketch.transform property. Is there any workaround that you can think of or a timeline that you can give for a sketch.transform2 property?

0 Likes
Message 6 of 6

BrianEkins
Mentor
Mentor

I don't think the problem with setting a sketch transform is the issue here. I think this one is just a matter of working out how to build up the correct transformation. I've been told the problem with setting the sketch transform is being addressed, but I don't know the specific release.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes