Mesh Trim_By_Plane Command

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Josean.G
Posts: 6
Joined: Thu Nov 21, 2019 1:03 am

Mesh Trim_By_Plane Command

Post by Josean.G »

Hi all,

I am cutting a messh by a plane using the GUI perfectly.
I have looked for a Python command that reproduces this instructions and I arrived to:
  • obj.Mesh.cut(list_of_points)
  • obj.Mesh.trim(list_of_points)
what by definition do not produce the same result, I guess...
I think I missundertand these two commands, because when I give them a list of points (vertices) Plane.shape.Vertexes, I get the following error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
ReferenceError: This object is immutable, you can not set any attribute or call a non const method


Is there a direct Python command that replicates Trim_By_plane GUI buttom processes?

Thank you,

OS: Windows 10 Version 2004
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.24276 (Git)
Build type: Release
Branch: releases/FreeCAD-0-19
Hash: a88db11e0a908f6e38f92bfc5187b13ebe470438
Python version: 3.8.6+
Qt version: 5.15.1
Coin version: 4.0.1
OCC version: 7.5.0
User avatar
HakanSeven12
Veteran
Posts: 1481
Joined: Wed Feb 06, 2019 10:30 pm

Re: Mesh Trim_By_Plane Command

Post by HakanSeven12 »

Code: Select all

mesh = obj.Mesh.copy()
obj.Mesh = mesh.trim(point_list, 1)
Josean.G
Posts: 6
Joined: Thu Nov 21, 2019 1:03 am

Re: Mesh Trim_By_Plane Command

Post by Josean.G »

Hi,

thank you for your answer

Unfortnately I receive a NoneType Object error.
I will explain the problem, I am probably doing something wrong.
I am try to trim a FC generated mesh with lowest and highest Z coordinate being: 0 and 32000 mm. I wan to trim it with a plane located in 10 m = 10000 mm
obj = doc.getObject("Mesh")
mesh = obj.Mesh.copy()

v0 = App.Vector(100,100,10).multiply(1e3)
v1 = App.Vector(-100,100,10).multiply(1e3)
v2 = App.Vector(-100,-100,10).multiply(1e3)
v3 = App.Vector(100,-100,10).multiply(1e3)
v4 = App.Vector(100,100,10).multiply(1e3)
points = [v0, v1, v2, v3, v4]

obj.Mesh = mesh.trim(points, 1)

Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: type must be 'Mesh', not NoneType
I am not sure if I have to give the v4 = v0 in the points array, I guess I should to close the polygon. Anyway using up to v3 also fails for me.

Do you find any mistake?

Thank you,
Josean.G
Posts: 6
Joined: Thu Nov 21, 2019 1:03 am

Re: Mesh Trim_By_Plane Command

Post by Josean.G »

Hi all,
I am not sure how to use the function trim.
I am trying to make it work trimming a cylinder mesh with a set of points at z = 0, but I have not been able with this script.
Could anyone explain to me how should I do this using python?
# Trim mesh script
draft = 0.0
l = 100.0

doc = App.ActiveDocument
obj = doc.getObject("Cylinder")
mesh= obj.Mesh.copy()

v0 = App.Vector(l,l,draft).multiply(1e3)
v1 = App.Vector(-l,l,draft).multiply(1e3)
v2 = App.Vector(-l,-l,draft).multiply(1e3)
v3 = App.Vector(l,-l,draft).multiply(1e3)
v4 = App.Vector(l,l,draft).multiply(1e3)

points = [v0, v1, v2, v3, v4]

mesh.trim(points,1)

__mesh__= doc.addObject("Mesh::Feature","Trimmed_Mesh")
#__mesh__.Mesh = mesh.trim(points,1)
__mesh__.Mesh = mesh
Thank you,
Josean
Attachments
MeshTrim_Cylinder.FCStd
Simple mesh of a cylinder
(64.22 KiB) Downloaded 33 times
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Mesh Trim_By_Plane Command

Post by heda »

this is foss after all, so before you are banging your head too much against the wall,
have you considered that it simply does not work as "announced"?

I do not know if it works or not, but your attempts suggest that it does not...

at least there seems to be some differences in py signatures and c signatures...
https://freecad.github.io/SourceDoc/d8/ ... dd24f6a773

maybe try to find another way to do it in python? like make a box and cut, or something?

btw: not that it matters much, but code in posts is generally wrapped with code tags, the one next to the quote tag ;)
wmayer
Founder
Posts: 20298
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Mesh Trim_By_Plane Command

Post by wmayer »

In C++ we have a function to trim a mesh by a plane but it was not yet exposed to Python.

With git commit 837de28e9ee21f you can do this in Python now.

The trimByPlane() functions expects two vectors to define a plane. It's the base point (i.e any point that lies on the plane) and the normal vector of the plane.
Post Reply