was trying out some sim stuff
This commit is contained in:
39387
PythonSim/SensorContext.STEP
Normal file
39387
PythonSim/SensorContext.STEP
Normal file
File diff suppressed because it is too large
Load Diff
102
PythonSim/solidworks_viewer.py
Normal file
102
PythonSim/solidworks_viewer.py
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
"""
|
||||||
|
SolidWorks File Viewer
|
||||||
|
Attempts to visualize SolidWorks files (.sldprt, .sldasm)
|
||||||
|
Note: SLDPRT files are proprietary - converting to STEP is recommended
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def convert_sldprt_info():
|
||||||
|
"""
|
||||||
|
Provide information about converting SLDPRT files
|
||||||
|
"""
|
||||||
|
print("=" * 60)
|
||||||
|
print("SLDPRT File Format Notice")
|
||||||
|
print("=" * 60)
|
||||||
|
print("\n.sldprt files are proprietary SolidWorks binary formats.")
|
||||||
|
print("\nRECOMMENDED APPROACH:")
|
||||||
|
print("1. Open your part in SolidWorks")
|
||||||
|
print("2. File → Save As → STEP (*.step, *.stp)")
|
||||||
|
print("3. Use step_viewer.py to visualize")
|
||||||
|
print("\nAlternatively, you can batch convert using SolidWorks API")
|
||||||
|
print("or use free converters like FreeCAD.\n")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
|
||||||
|
def batch_convert_with_freecad(sldprt_file):
|
||||||
|
"""
|
||||||
|
Attempt to convert SLDPRT to STEP using FreeCAD (if installed)
|
||||||
|
|
||||||
|
Args:
|
||||||
|
sldprt_file (str): Path to the SLDPRT file
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: Path to the converted STEP file, or None if failed
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
import FreeCAD
|
||||||
|
import Import
|
||||||
|
|
||||||
|
# Load the SLDPRT file
|
||||||
|
print(f"Attempting to load: {sldprt_file}")
|
||||||
|
doc = FreeCAD.newDocument()
|
||||||
|
Import.insert(sldprt_file, doc.Name)
|
||||||
|
|
||||||
|
# Export as STEP
|
||||||
|
base_name = os.path.splitext(sldprt_file)[0]
|
||||||
|
step_file = f"{base_name}_converted.step"
|
||||||
|
|
||||||
|
print(f"Converting to: {step_file}")
|
||||||
|
Import.export(doc.Objects, step_file)
|
||||||
|
|
||||||
|
FreeCAD.closeDocument(doc.Name)
|
||||||
|
print(f"Successfully converted to STEP format!")
|
||||||
|
return step_file
|
||||||
|
|
||||||
|
except ImportError:
|
||||||
|
print("\nFreeCAD not found. Install it with:")
|
||||||
|
print(" conda install -c conda-forge freecad")
|
||||||
|
print("\nOr download from: https://www.freecad.org/")
|
||||||
|
return None
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\nError during conversion: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Main entry point"""
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
print("Usage: python solidworks_viewer.py <path_to_sldprt_file>")
|
||||||
|
print("\nExample:")
|
||||||
|
print(" python solidworks_viewer.py model.sldprt")
|
||||||
|
convert_sldprt_info()
|
||||||
|
return
|
||||||
|
|
||||||
|
sldprt_file = sys.argv[1]
|
||||||
|
|
||||||
|
if not os.path.exists(sldprt_file):
|
||||||
|
print(f"Error: File not found: {sldprt_file}")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Check file extension
|
||||||
|
ext = os.path.splitext(sldprt_file)[1].lower()
|
||||||
|
if ext not in ['.sldprt', '.sldasm']:
|
||||||
|
print(f"Warning: File extension '{ext}' is not a SolidWorks format")
|
||||||
|
|
||||||
|
print("\nAttempting conversion to STEP format...\n")
|
||||||
|
|
||||||
|
# Try to convert using FreeCAD
|
||||||
|
step_file = batch_convert_with_freecad(sldprt_file)
|
||||||
|
|
||||||
|
if step_file and os.path.exists(step_file):
|
||||||
|
print("\nWould you like to visualize the converted STEP file?")
|
||||||
|
print(f"Run: python PythonSim/step_viewer.py {step_file}")
|
||||||
|
else:
|
||||||
|
print("\n" + "=" * 60)
|
||||||
|
convert_sldprt_info()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
162
PythonSim/step_viewer.py
Normal file
162
PythonSim/step_viewer.py
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
"""
|
||||||
|
STEP File Viewer for SolidWorks exports
|
||||||
|
Visualize 3D CAD models in an interactive 3D environment
|
||||||
|
"""
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
from OCC.Core.STEPControl import STEPControl_Reader
|
||||||
|
from OCC.Core.IFSelect import IFSelect_RetDone
|
||||||
|
from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh
|
||||||
|
from OCC.Core.TopExp import TopExp_Explorer
|
||||||
|
from OCC.Core.TopAbs import TopAbs_FACE
|
||||||
|
from OCC.Core.BRep import BRep_Tool
|
||||||
|
from OCC.Core.gp import gp_Pnt, gp_Ax2, gp_Dir
|
||||||
|
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeSphere
|
||||||
|
from OCC.Core.Quantity import Quantity_Color, Quantity_TOC_RGB
|
||||||
|
from OCC.Display.SimpleGui import init_display
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def load_step_file(filename):
|
||||||
|
"""
|
||||||
|
Load a STEP file and return the shape
|
||||||
|
|
||||||
|
Args:
|
||||||
|
filename (str): Path to the STEP file
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
TopoDS_Shape: The loaded 3D shape
|
||||||
|
"""
|
||||||
|
step_reader = STEPControl_Reader()
|
||||||
|
status = step_reader.ReadFile(filename)
|
||||||
|
|
||||||
|
if status != IFSelect_RetDone:
|
||||||
|
raise Exception(f"Error reading STEP file: {filename}")
|
||||||
|
|
||||||
|
# Transfer the contents of the STEP file to the shape
|
||||||
|
step_reader.TransferRoots()
|
||||||
|
shape = step_reader.OneShape()
|
||||||
|
|
||||||
|
print(f"Successfully loaded: {filename}")
|
||||||
|
return shape
|
||||||
|
|
||||||
|
|
||||||
|
def visualize_step(filename, background_color=(0.1, 0.1, 0.1), show_origin=True, origin_size=5.0):
|
||||||
|
"""
|
||||||
|
Visualize a STEP file in an interactive 3D viewer
|
||||||
|
|
||||||
|
Args:
|
||||||
|
filename (str): Path to the STEP file
|
||||||
|
background_color (tuple): RGB values (0-1) for background color
|
||||||
|
show_origin (bool): Whether to show origin marker
|
||||||
|
origin_size (float): Size of the origin marker sphere
|
||||||
|
"""
|
||||||
|
# Load the STEP file
|
||||||
|
shape = load_step_file(filename)
|
||||||
|
|
||||||
|
# Initialize the 3D display
|
||||||
|
display, start_display, add_menu, add_function_to_menu = init_display()
|
||||||
|
|
||||||
|
# Set background color using Quantity_Color
|
||||||
|
bg_color = Quantity_Color(background_color[0], background_color[1], background_color[2], Quantity_TOC_RGB)
|
||||||
|
display.View.SetBackgroundColor(bg_color)
|
||||||
|
|
||||||
|
# Display the shape
|
||||||
|
display.DisplayShape(shape, update=True)
|
||||||
|
|
||||||
|
# Add origin marker
|
||||||
|
if show_origin:
|
||||||
|
origin_point = gp_Pnt(0, 0, 0)
|
||||||
|
origin_marker = BRepPrimAPI_MakeSphere(origin_point, origin_size).Shape()
|
||||||
|
origin_color = Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB) # Red
|
||||||
|
display.DisplayShape(origin_marker, color=origin_color, update=True)
|
||||||
|
print(f"\nOrigin marker (red sphere) displayed at (0, 0, 0) with radius {origin_size}")
|
||||||
|
|
||||||
|
# Fit the view to show the entire model
|
||||||
|
display.FitAll()
|
||||||
|
|
||||||
|
print("\nControls:")
|
||||||
|
print(" - Left mouse button: Rotate")
|
||||||
|
print(" - Middle mouse button: Pan")
|
||||||
|
print(" - Right mouse button: Zoom")
|
||||||
|
print(" - F: Fit all")
|
||||||
|
print(" - ESC: Exit")
|
||||||
|
|
||||||
|
# Start the display loop
|
||||||
|
start_display()
|
||||||
|
|
||||||
|
|
||||||
|
def get_mesh_data(shape, linear_deflection=0.1):
|
||||||
|
"""
|
||||||
|
Extract mesh data (vertices and triangles) from a shape
|
||||||
|
Useful for custom rendering or analysis
|
||||||
|
|
||||||
|
Args:
|
||||||
|
shape: TopoDS_Shape object
|
||||||
|
linear_deflection (float): Mesh quality (lower = finer mesh)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
tuple: (vertices, triangles) as numpy arrays
|
||||||
|
"""
|
||||||
|
# Mesh the shape
|
||||||
|
mesh = BRepMesh_IncrementalMesh(shape, linear_deflection)
|
||||||
|
mesh.Perform()
|
||||||
|
|
||||||
|
vertices = []
|
||||||
|
triangles = []
|
||||||
|
vertex_index = 0
|
||||||
|
|
||||||
|
# Explore all faces
|
||||||
|
explorer = TopExp_Explorer(shape, TopAbs_FACE)
|
||||||
|
while explorer.More():
|
||||||
|
face = explorer.Current()
|
||||||
|
location = face.Location()
|
||||||
|
facing = BRep_Tool.Triangulation(face, location)
|
||||||
|
|
||||||
|
if facing:
|
||||||
|
# Get transformation
|
||||||
|
trsf = location.Transformation()
|
||||||
|
|
||||||
|
# Extract vertices
|
||||||
|
for i in range(1, facing.NbNodes() + 1):
|
||||||
|
pnt = facing.Node(i)
|
||||||
|
pnt.Transform(trsf)
|
||||||
|
vertices.append([pnt.X(), pnt.Y(), pnt.Z()])
|
||||||
|
|
||||||
|
# Extract triangles
|
||||||
|
for i in range(1, facing.NbTriangles() + 1):
|
||||||
|
triangle = facing.Triangle(i)
|
||||||
|
n1, n2, n3 = triangle.Get()
|
||||||
|
triangles.append([
|
||||||
|
vertex_index + n1 - 1,
|
||||||
|
vertex_index + n2 - 1,
|
||||||
|
vertex_index + n3 - 1
|
||||||
|
])
|
||||||
|
|
||||||
|
vertex_index += facing.NbNodes()
|
||||||
|
|
||||||
|
explorer.Next()
|
||||||
|
|
||||||
|
return np.array(vertices), np.array(triangles)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Main entry point for the STEP viewer"""
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
print("Usage: python step_viewer.py <path_to_step_file>")
|
||||||
|
print("\nExample:")
|
||||||
|
print(" python step_viewer.py model.step")
|
||||||
|
print(" python step_viewer.py C:\\Models\\assembly.stp")
|
||||||
|
return
|
||||||
|
|
||||||
|
step_file = sys.argv[1]
|
||||||
|
|
||||||
|
try:
|
||||||
|
visualize_step(step_file)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error: {e}")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user