You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python脚本实现FreeCAD中STEP文件的自动旋转与截图?

Hey there! I see you're getting started with FreeCAD Python scripting and ran into some issues with controlling object placement and taking screenshots. Let's break this down and fix it together.

Solution for FreeCAD Python Script: Capture Object Placement & Screenshots

First, let's fix your rotation code issue

Your original loop tried to use App.Rotation(App.Vector(i,j,k),1), but that's not how you set individual X/Y/Z rotation angles. That syntax creates a rotation around the vector (i,j,k) by 1 degree, not rotating i degrees around X, j around Y, and k around Z. That's why your code wasn't behaving as expected.

Step-by-step working code

Here's a practical, optimized script that handles rotation, captures placement data, and saves screenshots properly:

import FreeCAD
import FreeCADGui
import math
import os

# Get your document and STEP object
doc = FreeCAD.getDocument("mySTEP")
obj = doc.getObject("Part_Feature")

# Create a folder to store screenshots (avoids file clutter)
os.makedirs("step_screenshots", exist_ok=True)

# Define a rotation step (360*360*360 is way too many iterations! Use a reasonable step)
rotation_step = 45  # Adjust this to get more/less frequent screenshots

# Loop through rotation angles (X, Y, Z axes)
for x_deg in range(0, 360, rotation_step):
    for y_deg in range(0, 360, rotation_step):
        for z_deg in range(0, 360, rotation_step):
            # Set the object's placement: position (0,0,0) + X/Y/Z rotation angles
            obj.Placement = FreeCAD.Placement(
                FreeCAD.Vector(0, 0, 0),  # Adjust this if you need different positions
                FreeCAD.Rotation(x_deg, y_deg, z_deg)  # Euler angles in degrees
            )
            
            # Refresh the GUI to show the updated object pose
            FreeCADGui.ActiveDocument.ActiveView.fitAll()  # Optional: ensures object is centered
            FreeCADGui.updateGui()
            
            # Capture and print placement/rotation data
            position = obj.Placement.Base
            rotation = obj.Placement.Rotation
            # Convert Euler angles from radians to degrees for readability
            rot_xyz_deg = (
                math.degrees(rotation.EulerAngles[0]),
                math.degrees(rotation.EulerAngles[1]),
                math.degrees(rotation.EulerAngles[2])
            )
            print(f"Position (X,Y,Z): ({position.X:.2f}, {position.Y:.2f}, {position.Z:.2f})")
            print(f"Rotation (X,Y,Z degrees): ({rot_xyz_deg[0]:.1f}, {rot_xyz_deg[1]:.1f}, {rot_xyz_deg[2]:.1f})\n")
            
            # Save screenshot with descriptive filename
            screenshot_filename = f"step_screenshots/rot_x{x_deg}_y{y_deg}_z{z_deg}.png"
            # Save image with 800x600 resolution (adjust as needed)
            FreeCADGui.ActiveDocument.ActiveView.saveImage(screenshot_filename, 800, 600)

Important Notes to Avoid Headaches

  • Skip the full 360x360x360 loop: That would generate over 4.6 million screenshots—way too slow and storage-heavy. Use a rotation_step like 45 or 90 degrees to keep it manageable.
  • Always refresh the GUI: FreeCADGui.updateGui() ensures the view updates before you take a screenshot. Without this, you'll just get repeated shots of the initial pose.
  • Rotation precision: If you need explicit control over rotation order (e.g., rotate X first, then Y, then Z), use this syntax instead:
    FreeCAD.Rotation(
        FreeCAD.Vector(1,0,0), x_deg,  # Rotate X axis by x_deg
        FreeCAD.Vector(0,1,0), y_deg,  # Rotate Y axis by y_deg
        FreeCAD.Vector(0,0,1), z_deg   # Rotate Z axis by z_deg
    )
    
  • Position customization: Modify the FreeCAD.Vector(0,0,0) in the Placement to move the object to different positions if needed.

内容的提问来源于stack exchange,提问作者teslaUbuntu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:48:14