2024-05-13
Freecad Python Scripting

Reference:

https://wiki.freecad.org/FreeCAD_Scripting_Basics

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import Part
doc = FreeCAD.ActiveDocument
# list all objects
all_objects = doc.Objects
# list all names
all_object_names = [it.Name for it in all_objects]
# get object by name
obj = doc.getObject("myObjectName")
# get vertex point
vertex_point = obj.Shape.Vertexes[0].Point
# create new line
new_line = Part.makeLine((-200, -200, 0), (200, 200, 0))
# insert the line
Part.show(new_line)
# recompute the document
doc.recompute()

Draw squares within specific bounds:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
margin = 15
# Define the dimensions of the square area
x_min = -210.134 + margin
x_max = -84.134 - margin
y_min = -140.0997 + margin
y_max = -14.0997 - margin
z = 0
# Define the number of squares in each row and column
num_squares = 10
# Calculate the side length of each square
x_length = (x_max - x_min) / num_squares
y_length = (y_max - y_min) / num_squares
margin_portion = 0.17
hole_portion = 1 - 2 * margin_portion
# Create the squares
for i in range(num_squares):
for j in range(num_squares):
x_start = x_min + i * x_length + x_length * margin_portion
y_start = y_min + j * y_length + y_length * margin_portion
square_points = [
(x_start, y_start, z),
(x_start + x_length * hole_portion, y_start, z),
(x_start + x_length * hole_portion, y_start + y_length * hole_portion, z),
(x_start, y_start + y_length * hole_portion, z),
(x_start, y_start, z), # to make it closed
]
square = Part.makePolygon(square_points)
Part.show(square)

Draw circles with specific bounds:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
margin = 15
# Define the dimensions of the square area
x_min = -210.134 + margin
x_max = -84.134 - margin
y_min = -140.0997 + margin
y_max = -14.0997 - margin
z = 0
# Define the number of circles in each row and column
num_circles = 10
# Calculate the side length of each circle
x_length = (x_max - x_min) / num_circles
y_length = (y_max - y_min) / num_circles
margin_portion = 0.17
radius_portion = 0.5 - margin_portion
radius = x_length * radius_portion
direction = App.Vector(0, 0, 1)
# Create the squares
for i in range(num_circles):
for j in range(num_circles):
x_center = x_min + i * x_length + x_length * 0.5
y_center = y_min + j * y_length + y_length * 0.5
circle = Part.makeCircle(radius, App.Vector(x_center, y_center, 0), direction)
Part.show(circle)

Read More

2024-01-06
Unbuffered Python

When reading bytes asynchronously from stdout using asyncio.create_subprocess_exec, the program has to be unbuffered.

1
2
python3 -u <script_path>

1
2
3
4
5
6
7
8
9
10
11
# this can only make `print` into unbuffered
import builtins
import copy
old_print = copy.copy(builtins.print)
def custom_print(*args, **kwargs):
if 'flush' not in kwargs:
kwargs['flush'] = True
old_print(*args, **kwargs)
# Override the built-in print function with the custom function
builtins.print = custom_print

Read More

2022-12-18
Telegram Bot: Pyrogram

pyrogram repo

you can act both with a user account and a bot account

Read More

2022-12-02
Exploring Python Libraries And Resources For Nmap Network Scanning

nmap python scripting

python3-nmap and doc

doc of nmapthon python scriptable nse

python-nmap

Read More

2022-11-11
Call Python In Clojure, Clojure-Python Bridge

libpython-clj deep python integration in clojure

embed clojure in python call clojure in python

Read More

2022-08-10
Python Suggest Binary File Name Extension

Detect media file corruption, Python suggest binary file name extension

to rule out those corrupted media files, or unplayable files. maybe simply by parsing these files is not enough, we need a dedicated file corruption detector.

to truncate these files and see errors produced by media readers. use text file with media file extension to test them.

Read More