Extrude DXF which has a lot of details
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello, please help me solve the problem. I'm not a programmer, and I tried to hire 3 freelancers but no one could do what I needed.
This is all I could implement using the script in the ChatGPT.
I need the script to extrude individual parts in the DXF file.
I can't figure out how to specify the definition and extrusion of profiles. I have a lot of similar work and would like to automate it. I really ask you to help me solve this problem and am happy to reward you for your help!
Profiles in DXF what need to extrude.
What i get after run script :
How can this be done correctly and is it possible?
Don't pay attention to the other DXF in the code, everything is fine with them while working.
Here my code :
import adsk.core, adsk.fusion, adsk.cam, traceback, os
def import_dxf(file_path, root_comp):
app = adsk.core.Application.get()
import_manager = app.importManager
if not os.path.isfile(file_path):
raise RuntimeError(f"File not found: {file_path}")
dxf_options = import_manager.createDXF2DImportOptions(file_path, root_comp.xYConstructionPlane)
try:
# Import the DXF file into the current design
import_manager.importToTarget(dxf_options, root_comp)
# Return the last sketch added to the root component
sketch = root_comp.sketches.item(root_comp.sketches.count - 1)
except RuntimeError as e:
raise RuntimeError(f"Failed to import DXF file '{file_path}': {e}")
except Exception as e:
raise RuntimeError(f"Unexpected error while importing DXF file '{file_path}': {e}")
return sketch
def perform_extrusion(sketch, distance, operation):
app = adsk.core.Application.get()
design = app.activeProduct
root_comp = design.rootComponent
# Get all profiles
profiles = sketch.profiles
if profiles.count == 0:
raise RuntimeError("No profiles found in the sketch.")
extrudes = root_comp.features.extrudeFeatures
for i in range(profiles.count):
profile = profiles.item(i)
# Create an extrusion input
ext_input = extrudes.createInput(profile, adsk.fusion.FeatureOperations.CutFeatureOperation if operation == 'cut' else adsk.fusion.FeatureOperations.JoinFeatureOperation)
# Define the extent of the extrusion (distance in mm)
distance_value = adsk.core.ValueInput.createByReal(distance)
ext_input.setDistanceExtent(False, distance_value)
# Create the extrusion
extrude = extrudes.add(ext_input)
def combine_occurrences(root_comp):
app = adsk.core.Application.get()
design = app.activeProduct
root_comp = design.rootComponent
# Collect all occurrences
all_occurrences = root_comp.allOccurrences
# Create a collection to store all bodies
all_bodies = adsk.core.ObjectCollection.create()
# Iterate through each occurrence and collect its bodies
for occurrence in all_occurrences:
component = occurrence.component
bodies = component.bRepBodies
for body in bodies:
all_bodies.add(body)
if all_bodies.count < 2:
raise RuntimeError("Not enough bodies to combine.")
# Use the first body as the target and the rest as tools
target_body = all_bodies.item(0)
tool_bodies = adsk.core.ObjectCollection.create()
for i in range(1, all_bodies.count):
tool_bodies.add(all_bodies.item(i))
# Create a combine feature input
combine_features = root_comp.features.combineFeatures
combine_input = combine_features.createInput(target_body, tool_bodies)
# Set the operation type
combine_input.operation = adsk.fusion.FeatureOperations.JoinFeatureOperation
# Create the combine feature
combine_feature = combine_features.add(combine_input)
def save_as_stl(output_path, filename):
app = adsk.core.Application.get()
design = app.activeProduct
export_manager = design.exportManager
stl_options = export_manager.createSTLExportOptions(design.rootComponent)
stl_options.meshRefinement = adsk.fusion.MeshRefinementSettings.MeshRefinementHigh
stl_options.filename = os.path.join(output_path, filename + '.stl')
try:
export_manager.execute(stl_options)
except RuntimeError as e:
raise RuntimeError(f"Failed to export STL: {e}")
def main():
app = adsk.core.Application.get()
ui = app.userInterface
project_path = r"C:\Cuts" # Use raw string to avoid escape issues
output_path = r"C:\Cuts" # Use raw string to avoid escape issues
if not os.path.exists(output_path):
os.makedirs(output_path)
file_paths = [
("3mm.dxf", 0.3, 'join'),
("7mm.dxf", 0.7, 'join'),
("10mm.dxf", 1.0, 'join'),
("cut.dxf", 1.2, 'cut'),
("stamp.dxf", 0.8, 'join'),
("support.dxf", 0.3, 'join')
]
design = app.activeProduct
root_comp = design.rootComponent
for file_name, distance, operation in file_paths:
file_path = os.path.join(project_path, file_name)
try:
sketch = import_dxf(file_path, root_comp)
perform_extrusion(sketch, distance, operation)
except RuntimeError as e:
ui.messageBox(f"Failed to import or process DXF file '{file_name}': {e}")
continue
except Exception as e:
ui.messageBox(f"Unexpected error: {e}")
continue
try:
combine_occurrences(root_comp)
save_as_stl(output_path, os.path.basename(project_path))
ui.messageBox('Successfully imported, processed, and saved the STL file.')
except RuntimeError as e:
ui.messageBox(f"Failed to save STL: {e}")
try:
main()
except:
app = adsk.core.Application.get()
ui = app.userInterface
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))