Script to add standard offset to sketch lines

schornagel
Explorer

Script to add standard offset to sketch lines

schornagel
Explorer
Explorer

Hi, when making glass in lead, you need to take into account that between each piece of glass is a lead core of 1.2mm. Especially when creating complex designs, this is a pain to calculate by hand. The solution; Fusion;) Now the ideal situation would be to create a sketch with the desired design and then offset every line with 0.6mm to both sides to create the needed space for the lead core. 

Unfortunately, the Fusion360's offset function in sketch mode only allows to select continuous lines, where I'm looking to add offsets to all lines of a sketch that are interconnected. 

 

I'm wrapping my head around how to create an API script that adds an offset line to each line in a sketch (that these offset lines sometimes overlap is not a problem for me). Code below is what I have so far, got is working up until getting the lines, but completely stuck at how to create these offset lines. I found some code examples offsets in faces, but none in terms of sketches.

 

Any help would be very much appreciated. Happy to share my code afterwards for anyone in the same situation:)

 

 

 

app = adsk.core.Application.get()
ui  = app.userInterface
design = app.activeProduct

# Get the root component of the active design.
rootComp = design.rootComponent

# Get the sketch in editing mode
sketch = rootComp.sketches[5]
ui.activeSelections.clear()
ui.activeSelections.add(sketch)
skCmd = ui.commandDefinitions.itemById('SketchActivate')
skCmd.execute()

# Get all sketch lines
lines = sketch.sketchCurves.sketchLines

# Create inputEntities collection and define offset distance
inputEntities = adsk.core.ObjectCollection.create()
distance = adsk.core.ValueInput.createByString('1 cm')

# Loop through lines and add to input entities
for line in lines:
        inputEntities.add(lines[0])

# Create offsets for each line
offsetFeatures = features.offsetFeatures
offsetInput = offsetFeatures.createInput(inputEntities, distance, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
offsetFeatures.add(offsetInput)

 

 

 

0 Likes
Reply
877 Views
2 Replies
Replies (2)

schornagel
Explorer
Explorer

So this worksSo this worksThis doesn't workThis doesn't workAlright, I'm pretty close now. Code works for neatly closed shapes (such as a triangle). However, if I draw a line in this triangle connecting two corners I still get an error because Fusion is confused and still wants to follow a chained line. What I want to do (and what I would expect from the way this code processes each line which is in objs) is that each line would be separately processed, so there would not be a problem. Again, any help is welcome. 

 

 

 

app = adsk.core.Application.get()
        ui  = app.userInterface
        ui.messageBox('Hello script')

        #doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        design = app.activeProduct

        # Get the root component of the active design.
        rootComp = design.rootComponent
        
        # Get first sketch and related lines
        sketch = rootComp.sketches[0]
        lines = sketch.sketchCurves.sketchLines
   
        # create ObjectCollection and fill with lines
        objs = adsk.core.ObjectCollection.create()
        objs.clear()
        [objs.add(l) for l in lines]

        # Not clear how this is used from documentation, but a 3D point is required for direction
        centerPoint = adsk.core.Point3D.create(10, 10, 0)

        # Create the offset
        offset = 0.1
        sketch.offset(objs, centerPoint, offset)

 

 

 

0 Likes

BrianEkins
Mentor
Mentor

I think the question is, which side of the line do you want to offset to?  For a closed shape, it's more defined because you can offset to the inside or the outside.  But if you have a bunch of lines that aren't even necessarily connected, which side do you want to offset?  Or do you want to treat the existing line as the mid-plane and offset to each side one-half the desired thickness?

 

With lines, it would be fairly easy for you to draw new lines yourself that represents the offset of the existing line.  Again, you need to decide which side of the line to offset to.  And the other part is how do you want to trim the ends of the lines, if at all?

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