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: 

Sketch with text via API not exporting text but will manually

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
marylou.forsyth
178 Views, 2 Replies

Sketch with text via API not exporting text but will manually

Good day programmers!  I am asking for help as I am just not seeing what steps I am missing.  Please let me give the background.  We open a model via API script, we read in new parameters to update the model to, and update the model.  All processing is done via the API script.  My customer has requested a specific type of processing done, where the model has a few sketches, to delete all sketches not named in a list, and export the model as a DXF.  The logic I wrote to address this can be identified as the DXF_Type_BPB (for it needs to be a DXF export with only the Body, Pen_lines and Bend_Lines {BPB})

 

The logic goes through and removes the sketches not needed, then exports out a file with just the pen-line (so I can verify in testing which actually looks great) and then exports out the body and the one sketch Pen_Line.  My problem is when the second export happens, I don't see the text.  

 

The manual export (fullPath + sketch.name + 'dxf') which is looking ok to me appears as this:

marylouforsyth_0-1679412485239.png

and at the top you can see the text data 'HV something'.

The export with the body, Pen_line though only creates this as I must be missing something major:

marylouforsyth_1-1679412590961.png

Here is the code (please ignore the section that says else: DXF_Type_BPB == False ) AND please forgive the horrible loop where I flush out the un-needed sketches, it will be re-written.

 

I thought this line was going to give what I needed but sadly no:

[sketchEntities.add(ent) for ent in sketch.sketchTexts]

One more thing, there is logic to adjust the scaling but thats been working fine.  

 

#we call this right before the code in question --> : updateParametricModel(tag,value,userParams,False)


else: # its a punch so export the drawing scaled correctly
global DXF_Type_BPB
# flag if problem of no cam found when making a DXF
Stop_it = False

folder = "W:\\SharedData\\Fusion_360\\DXF\\"
fullPath = os.path.join(folder, order)

if (DXF_Type_BPB == True):
Total_Count_Sketches = 0
targetSketches = ['Pen_Line','Pen_line','pen_line','Bend_Line','Bend_line','bend_line']
DFX_Generated = True
design : adsk.fusion.Design = app.activeProduct
root: adsk.fusion.Component = design.rootComponent
master: adsk.fusion.Sketch = root.sketches.add(root.xYConstructionPlane)
rootComp: adsk.fusion.Component = design.rootComponent

#### Check names of all sketches
#sketches = rootComp.sketches
#sketch_names = []
#i1 = 0
#for sk in sketches:
# print(str(sketches.item(i1).name))
# i1 = i1 + 1
#####

sketches = rootComp.sketches
sketch_names = []
Kludge_Counter = 0
Not_done = False
while Not_done == False:
Kludge_Counter = Kludge_Counter + 1
if (Kludge_Counter > 20):
Not_done = True
i1 = 0
for sk in sketches:
sketch = sketches.item(i1)
if (sketch.name in targetSketches):
#sketch_names.append(sketches.item(i1).name)
print(str(sketches.item(i1).name))
i1 = i1 + 1
else:
print(str(sketches.item(i1).name))
sketch.deleteMe()
break
# This matches the manually exported DXF,
# so things are great up to here ...I think...
sketch.saveAsDXF(fullPath + sketch.name + '.dxf')

####
sketches = rootComp.sketches
sketch_names = []
i1 = 0
for sk in sketches:
print(str(sketches.item(i1).name))
# nope master.project(sk)
i1 = i1 + 1
#####

sketchEntities: adsk.core.ObjectCollection = adsk.core.ObjectCollection.create()
for sketch in rootComp.sketches:
myname = sketch.name
print(myname)
[sketchEntities.add(ent) for ent in sketch.sketchCurves]
[sketchEntities.add(ent) for ent in sketch.sketchPoints]
[sketchEntities.add(ent) for ent in sketch.sketchTexts]

# Create new Sketch
master: adsk.fusion.Sketch = rootComp.sketches.add(rootComp.xYConstructionPlane)
master.name = 'Master'
# Project Body
targetBody: adsk.fusion.BRepBody = rootComp.bRepBodies[0]
project_result: adsk.core.ObjectCollection = master.project(targetBody)
if project_result.count < 1:
print('Failure of the body project')

# Include sketchEntities
master.include(sketchEntities)

# Project sketchEntities
master.project(sketchEntities)

else: # (DXF_Type_BPB == False)
design : adsk.fusion.Design = app.activeProduct
# new logic 2 statements added 02-28-2023
document = app.activeDocument
design = document.design
try:
rootComp = design.rootComponent
except:
msg = 'This model contains no CAM object, no DXF can be generated'
if (run_from_list == False):
# inform operator issue no cam object found
ui.messageBox(msg)
Stop_it = True
_flags.append(msg)
else:
# set a flag so we skip all further processing
Stop_it = True
print(msg)

if (Stop_it == False):
master: adsk.fusion.Sketch = rootComp.sketches.add(rootComp.xYConstructionPlane)
targetBody: adsk.fusion.BRepBody = rootComp.bRepBodies[0]
master.project(targetBody)

if (Stop_it == False):
# Scale the model if its in inches
if(in_inches == True):
basePt = master.sketchPoints.item(0)
scaleFactor = adsk.core.ValueInput.createByString("10 mm")
#scales = rootComp.features.scaleFeatures
scales = rootComp.features.scaleFeatures
#Create an uniformed input for scale feature input
inputUniformColl = adsk.core.ObjectCollection.create()
inputUniformColl.add(master)
scaleUniformInput = scales.createInput(inputUniformColl, basePt, scaleFactor)
scaleUniform = scales.add(scaleUniformInput)

# Special scaling for the other tool for MM models, updated 12-8-22
if(in_inches == False):
basePt = master.sketchPoints.item(0)
scaleFactor = adsk.core.ValueInput.createByString("1.0 in/64.516")
#scales = rootComp.features.scaleFeatures
scales = rootComp.features.scaleFeatures
#Create an uniformed input for scale feature input
inputUniformColl = adsk.core.ObjectCollection.create()
inputUniformColl.add(master)
scaleUniformInput = scales.createInput(inputUniformColl, basePt, scaleFactor)
scaleUniform = scales.add(scaleUniformInput)

# write the merged file
master.saveAsDXF(fullPath + '.dxf')
msg = '!! DXF file has been written !!'
DFX_Generated = True
#print(msg)
futil.log(msg)

 

I will drag and drop as this is hard to read.

 

Thank you for your time and assistance, I have looked and tried so many different things but I just can't resolve this.

 

 

2 REPLIES 2
Message 2 of 3
kandennti
in reply to: marylou.forsyth

Hi @marylou.forsyth .

 

If you want to include your code in the forum, this is a good way to display it.

1.png

 

When I tried, the project/include method was not available for SketchText objects.

Therefore, I created a sample that explodes and then projects the data.

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion

def run(context):
    ui = core.UserInterface.cast(None)
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface
        des: fusion.Design = app.activeProduct
        root: fusion.Component = des.rootComponent

        path = r'C:\temp\test.dxf'

        targetSketchNames = ['PEN_LINE','BEND_LINE']

        try:
            app.executeTextCommand(u'Transaction.Start export_project_sketch')

            export_project_sketch(
                targetSketchNames,
                root.xYConstructionPlane,
                path,
            )
        except:
            pass
        finally:
            app.executeTextCommand(u'Transaction.Abort')

        ui.messageBox('Done')

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


def export_project_sketch(
    targetSketchNames: list,
    supportPlane: fusion.ConstructionPlane,
    path: str,
) -> None:

    def get_sketch_entities(
        skt: fusion.Sketch,
    ) -> list:

        lst = [c for c in skt.sketchCurves]
        lst.extend([p for p in skt.sketchPoints])

        return lst

    def explode_sketchTexts(
        skt: fusion.Sketch,
    ) -> None:

        skt.isComputeDeferred = True

        sktTxtLst = [t for t in skt.sketchTexts]
        [t.explode() for t in sktTxtLst]
            
        skt.isComputeDeferred = False
    # *******

    app: core.Application = core.Application.get()
    des: fusion.Design = app.activeProduct
    root: fusion.Component = des.rootComponent

    sktLst = [skt for skt in root.sketches if skt.name.upper() in targetSketchNames]
    if len(sktLst) < 1:
        return

    [explode_sketchTexts(skt) for skt in sktLst]

    sktEnts = []
    [sktEnts.extend(get_sketch_entities(skt)) for skt in sktLst]
    if len(sktEnts) < 1:
        return

    sketchEntities: core.ObjectCollection = core.ObjectCollection.create()
    [sketchEntities.add(e) for e in sktEnts]

    master: fusion.Sketch = root.sketches.add(supportPlane)
    master.name = 'Master'

    master.project(sketchEntities)
    master.saveAsDXF(path)

 

Since it would be inconvenient if the original data were left exploded, we used a transaction of text commands to return the data to the state before processing.
However, since explode is very time-consuming, I do not think it will be useful in practice.

Message 3 of 3
marylou.forsyth
in reply to: kandennti

You are a wonder to behold!  You have nailed the issue, I have been working on this and you just brought it to a resolution, bless you!!  Yes, so right you are I spoke with the developers and they will explode the text in sketches so the API will not have too.  Would not have gotten here without your help, 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