I came across this fascinating project that uses a custom GPT model called “3D-Designer” to design any part for 3D printing. However, this model requires a ChatGPT Pro license that costs 20$ per month. Therefore, I decided to use a free alternative to replicate it. I used Copilot, which is accessible from the Microsoft Edge browser by clicking the button in the upper right corner.
Or using the bing search engine with any web browser. After entering the chat page, you have to choose the “more creative mode” option to access GPT-4.
Here are the main differences between ChatGPT and Copilot:
- ChatGPT and Copilot both use a large language model called GPT, but Copilot has an advantage over ChatGPT because it uses the more advanced GPT-4 model for free users.
- Copilot can access the latest information and web results through Bing, Microsoft's own search engine. ChatGPT, on the other hand, needs a $20 monthly subscription to search the web for information after 2022.
- ChatGPT Plus and Copilot Pro are optional subscriptions that offer priority access and other perks to users. ChatGPT Plus is for ChatGPT users, while Copilot Pro is for Copilot users.
- ChatGPT can generate longer and more creative responses, but the free version cannot verify its responses with external sources.
Here is the notebook I used to try the code https://colab.research.google.com/drive/1I2ZWyl4K6tRjlNqTVh6IN7kZJOXd4bkg?usp=sharing
I used a modified prompt, as I wanted to test the code in Google Colab and visualize the piece on my PC. I did not need a visualization, but I needed an stl file.
Using latest PythonOCC-Core library can you write a script that create a U-shaped bracket "ubracket" : - create a outer cube 40 x 30 x 20 mm - create a inner cube 30 x 30 x 20 mm - shift the inner cube 5 mm up and right - subtract the inner cube from the outer cube - drill a hole of radius 3 mm at position (0, 20, 10) with length 10 mm in x direction (1, 0, 0) - export ubracket to a STL file "ubracket.stl"
I got this code
# Import the required modules from PythonOCC-Core
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeCylinder
from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Cut
from OCC.Core.gp import gp_Pnt, gp_Vec, gp_Dir, gp_Ax2
from OCC.Core.STEPControl import STEPControl_Writer, STEPControl_AsIs
from OCC.Core.IFSelect import IFSelect_RetDone
# Create a outer cube 40 x 30 x 20 mm
outer_cube = BRepPrimAPI_MakeBox(40, 30, 20).Shape()
# Create a inner cube 30 x 30 x 20 mm
inner_cube = BRepPrimAPI_MakeBox(30, 30, 20).Shape()
# Shift the inner cube 5 mm up and right
inner_cube.Move(gp_Vec(5, 5, 0))
# Subtract the inner cube from the outer cube
ubracket = BRepAlgoAPI_Cut(outer_cube, inner_cube).Shape()
# Drill a hole of radius 3 mm at position (0,20,10) with length 10 mm in x direction (1,0,0)
hole = BRepPrimAPI_MakeCylinder(gp_Ax2(gp_Pnt(0, 20, 10), gp_Dir(1, 0, 0)), 3, 10).Shape()
ubracket = BRepAlgoAPI_Cut(ubracket, hole).Shape()
# Export ubracket to a STL file "ubracket.stl"
step_writer = STEPControl_Writer()
step_writer.Transfer(ubracket, STEPControl_AsIs)
status = step_writer.Write("ubracket.stl")
if status != IFSelect_RetDone:
raise IOError("Error while writing STL file")
I
tried it in colab and I got an error, so I asked Copilot:
"I get an error in line 15 TypeError: in method 'TopoDS_Shape_Move', argument 2 of type 'TopLoc_Location const &'"
And I received this message
I’m sorry to hear that you encountered an error in your script. 😞
According to the Open CASCADE Technology Documentation, the TopoDS_Shape_Move
method takes a TopLoc_Location
object as the second argument, which represents the shape’s local coordinate system1.However, in your code, you are passing a gp_Vec
object, which represents a vector in 3D space1. This is why you get a TypeError
, because the types do not match.
To fix this error, you need to convert your gp_Vec
object to a TopLoc_Location
object. One way to do this is to use the TopLoc_Location
constructor that takes a gp_Trsf
object as an argument1.A gp_Trsf
object is a geometric transformation that can be defined by a translation vector1.You can create a gp_Trsf
object from your gp_Vec
object by using the SetTranslation
method1.
Here is an example of how to do this:
# Import the required module
from OCC.Core.gp import gp_Trsf
# Create a gp_Vec object
vec = gp_Vec(5, 5, 0)
# Create a gp_Trsf object from the gp_Vec object
trsf = gp_Trsf()
trsf.SetTranslation(vec)
# Create a TopLoc_Location object from the gp_Trsf object
loc = TopLoc_Location(trsf)
# Use the TopLoc_Location object to move the inner cube
inner_cube.Move(loc)
I hope this helps you to resolve the error and run your script successfully. 😊
I made some changes to the code and ran it again. However, I encountered a new error and consulted Copilot for help. The error was in line 25: NameError: name ‘TopLoc_Location’ is not defined. Copilot gave me a new answer to fix the error.
I’m sorry to hear that you encountered another error in your script. 😞
This error means that Python does not recognize the name TopLoc_Location
as a valid identifier. This could be because you forgot to import it from the OCC.Core.TopLoc
module1, or because you misspelled it.
To fix this error, you need to make sure that you have the following line at the top of your script:
from OCC.Core.TopLoc import TopLoc_Location
This will import the TopLoc_Location
class from the OCC.Core.TopLoc
module and make it available in your script. Alternatively, you can import the whole module and use the qualified name:
import OCC.Core.TopLoc
# ...
loc = OCC.Core.TopLoc.TopLoc_Location(trsf)
I hope this helps you to resolve the error and run your script successfully. 😊
1: PythonOCC - 3D CAD/CAE/PLM development framework for the Python programming language
I ran the code successfully and downloaded the stl file. However, when I tried to open it, I encountered an error. I asked Copilot for help again:
I can't open the stl generated, is there any alternative way of generate it using pythonOCC library?
I’m sorry to hear that you have trouble generating an STL file using the pythonOCC library. 😞
According to the web search results that I found, there are a few ways to generate an STL file using the pythonOCC library. Here are some possible options:
- You can use the
write_stl_file
function from theOCC.Extend.DataExchange
module1.This function takes a shape and a file name as arguments and writes the shape as an STL file1. For example:
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeTorus
from OCC.Extend.DataExchange import write_stl_file
# create a torus shape
my_torus = BRepPrimAPI_MakeTorus(20.0, 10.0).Shape()
# write the shape as an STL file
write_stl_file(my_torus, "my_torus.stl")
- You can use the
STEPControl_Writer
class from theOCC.Core.STEPControl
module2.This class allows you to transfer a shape to a STEP file format and write it to a file2.You can then convert the STEP file to an STL file using another tool, such as FreeCAD3. For example:
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeTorus
from OCC.Core.STEPControl import STEPControl_Writer, STEPControl_AsIs
from OCC.Core.IFSelect import IFSelect_RetDone
# create a torus shape
my_torus = BRepPrimAPI_MakeTorus(20.0, 10.0).Shape()
# create a STEP writer
step_writer = STEPControl_Writer()
# transfer the shape to the STEP writer
step_writer.Transfer(my_torus, STEPControl_AsIs)
# write the shape as a STEP file
status = step_writer.Write("my_torus.step")
# check the status
if status != IFSelect_RetDone:
raise IOError("Error while writing STEP file")
I hope this helps you to find a suitable way to generate your STL file using the pythonOCC library. 😊
Using the write_stl_file method, I successfully created the part. Here is how it looks.
Here is the code with the unnecessary lines removed or commented out:
# Import the required modules from PythonOCC-Core
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeCylinder
from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Cut
from OCC.Core.gp import gp_Pnt, gp_Vec, gp_Dir, gp_Ax2
#from OCC.Core.STEPControl import STEPControl_Writer, STEPControl_AsIs
#from OCC.Core.IFSelect import IFSelect_RetDone
from OCC.Core.gp import gp_Trsf
import OCC.Core.TopLoc
from OCC.Extend.DataExchange import write_stl_file
# Create a outer cube 40 x 30 x 20 mm
outer_cube = BRepPrimAPI_MakeBox(40, 30, 20).Shape()
# Create a inner cube 30 x 30 x 20 mm
inner_cube = BRepPrimAPI_MakeBox(30, 30, 20).Shape()
# Create a gp_Vec object
vec = gp_Vec(5, 5, 0)
# Create a gp_Trsf object from the gp_Vec object
trsf = gp_Trsf()
trsf.SetTranslation(vec)
# Create a TopLoc_Location object from the gp_Trsf object
#loc = TopLoc_Location(trsf)
loc = OCC.Core.TopLoc.TopLoc_Location(trsf)
# Use the TopLoc_Location object to move the inner cube
inner_cube.Move(loc)
# Shift the inner cube 5 mm up and right
#inner_cube.Move(gp_Vec(5, 5, 0))
# Subtract the inner cube from the outer cube
ubracket = BRepAlgoAPI_Cut(outer_cube, inner_cube).Shape()
# Drill a hole of radius 3 mm at position (0,20,10) with length 10 mm in x direction (1,0,0)
hole = BRepPrimAPI_MakeCylinder(gp_Ax2(gp_Pnt(0, 20, 10), gp_Dir(1, 0, 0)), 3, 10).Shape()
ubracket = BRepAlgoAPI_Cut(ubracket, hole).Shape()
# Export ubracket to a STL file "ubracket.stl"
#step_writer = STEPControl_Writer()
#step_writer.Transfer(ubracket, STEPControl_AsIs)
#status = step_writer.Write("ubracket.stl")
#if status != IFSelect_RetDone:
# raise IOError("Error while writing STL file")
write_stl_file(ubracket, "ubracket.stl") # added this line to export the shape to STL
To sum up, by using the free alternative to ChatGPT, Copilot, and some patience, we can create 3D parts without spending much money or time.
Comments
Please log in or sign up to comment.