Approach to creating offsets for text

Approach to creating offsets for text

snapper194
Explorer Explorer
970 Views
4 Replies
Message 1 of 5

Approach to creating offsets for text

snapper194
Explorer
Explorer

Can someone recommend an approach to creating an outline (offset) around every text character in a sketch?

 

Here's what I'm trying to do.  If I have sketched text like "test", I want to be able to create an outline around each character in "test". 

 

So far my Python script explodes the sketch text, then I try looping through the profiles to create the offset for each character.  The first offset gets created correctly as you can see in the screenshot.  The problem is after that the profiles get recalculated automatically, which is messing things up.

 

Any suggestion on how to accomplish this?  I suspect I shouldn't be trying to do this through the profiles as they're constantly recalculated.

 

Here's the error:

snapper194_0-1625843193856.png

 

 

My code:

 

 

import adsk.core, adsk.fusion, traceback, time

def run(context):
	ui = None
	try: 
		text_height = 02.1

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

		doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
		design = adsk.fusion.Design.cast( app.activeProduct )

		#Prompts the user for the new Text   
		(returnValue, cancelled) = ui.inputBox( 'What text?', 'New text:', )

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

		allOccs = rootComp.occurrences
		transform = adsk.core.Matrix3D.create()

		# Create a component under root component
		occ1 = allOccs.addNewComponent(transform)
		subComp1 = occ1.component

		# this will be the component for the text
		subComp1.name = '{} - Arial'.format( returnValue )

		# Create a sketch in subComp 1
		sketches1 = subComp1.sketches
		sketch1 = sketches1.add(rootComp.xYConstructionPlane)

		# Get sketch texts 
		sketchTexts = sketch1.sketchTexts        
		
		# Create sketch text input
		point = adsk.core.Point3D.create(1.0, 1.0, 0)
		sketchTextInput = sketchTexts.createInput( returnValue, 1.0, point)
		sketchTextInput.fontName = 'Arial'
		sketchTextInput.height = text_height

		# Create sketch text
		sketchText = sketchTexts.add(sketchTextInput)

		# Explode the text
		sketchCurves = sketchText.explode()

		curvesForOffset = adsk.core.ObjectCollection.create()

		i = 0
		profile = adsk.fusion.Profile.cast(None)		
		for profile in sketch1.profiles:

			curvesForOffset.clear()			# clear the collection

			loop = profile.profileLoops.item(0)

			curves = loop.profileCurves
			for curve in curves:
				curve = adsk.fusion.ProfileCurve.cast(curve)
				entity = curve.sketchEntity
				curvesForOffset.add( entity )                

			if curvesForOffset.count > 0:
				sketch1.offset(curvesForOffset, adsk.core.Point3D.create(0,0,0), .3 )                        

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

 

 

0 Likes
Accepted solutions (1)
971 Views
4 Replies
Replies (4)
Message 2 of 5

BrianEkins
Mentor
Mentor
Accepted solution

You're right about the profiles being recalculated as the contents of the sketch change.  However, I think your code will work with a slight change.  You need two loops. The first will collect the geometry from the profiles before you start adding more geometry, and the second loops will create the offsets, using the collected geometry.

 

Without testing it, here's what I think might work.

import adsk.core, adsk.fusion, traceback, time

def run(context):
	ui = None
	try: 
		text_height = 02.1

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

		doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
		design = adsk.fusion.Design.cast( app.activeProduct )

		#Prompts the user for the new Text   
		(returnValue, cancelled) = ui.inputBox( 'What text?', 'New text:', )

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

		allOccs = rootComp.occurrences
		transform = adsk.core.Matrix3D.create()

		# Create a component under root component
		occ1 = allOccs.addNewComponent(transform)
		subComp1 = occ1.component

		# this will be the component for the text
		subComp1.name = '{} - Arial'.format( returnValue )

		# Create a sketch in subComp 1
		sketches1 = subComp1.sketches
		sketch1 = sketches1.add(rootComp.xYConstructionPlane)

		# Get sketch texts 
		sketchTexts = sketch1.sketchTexts        
		
		# Create sketch text input
		point = adsk.core.Point3D.create(1.0, 1.0, 0)
		sketchTextInput = sketchTexts.createInput( returnValue, 1.0, point)
		sketchTextInput.fontName = 'Arial'
		sketchTextInput.height = text_height

		# Create sketch text
		sketchText = sketchTexts.add(sketchTextInput)

		# Explode the text
		sketchCurves = sketchText.explode()

		# Get the curves.
		curveSets = []
		i = 0
		profile = adsk.fusion.Profile.cast(None)		
		for profile in sketch1.profiles:
			curvesForOffset = adsk.core.ObjectCollection.create()

			loop = profile.profileLoops.item(0)
			curves = loop.profileCurves
			for curve in curves:
				curve = adsk.fusion.ProfileCurve.cast(curve)
				entity = curve.sketchEntity
				curvesForOffset.add( entity )                

			curveSets.append(curvesForOffset)

		for curveSet in curveSets:
			if curveSet.count > 0:
				sketch1.offset(curveSet, adsk.core.Point3D.create(0,0,0), .3 )                        

	except:
		if ui:
			ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 3 of 5

snapper194
Explorer
Explorer

Thank you, that's working perfectly! 

 

I tried something similar before, but I think I was trying to save the profiles in a list before I started modifying them, but for some reason that wasn't working.

0 Likes
Message 4 of 5

BrianEkins
Mentor
Mentor

Saving the profiles won't work because, as you've seen they're being recomputed as new geometry is added, so the profiles you saved at first no longer exist as you start adding geometry.  This new approach saves the original sketch geometry, which isn't changing as you add new geometry, so it continues to work.

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

guillermo.baselga
Participant
Participant

I had to log in just to thank you, the offset command is horribly documented and your code solved a problem that had me banging my head against a wall for days 🙂

0 Likes