Part_Measure_Linear output in macro
Forum rules
and Helpful information
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help
Also, be nice to others! Read the FreeCAD code of conduct!
Also, be nice to others! Read the FreeCAD code of conduct!
Part_Measure_Linear output in macro
Hello
How does one refer to the result/output of a Part_Measure_Linear command in a macro?
Thank you in advance,
Nuvonic
How does one refer to the result/output of a Part_Measure_Linear command in a macro?
Thank you in advance,
Nuvonic
-
- Veteran
- Posts: 5513
- Joined: Thu Apr 05, 2018 1:53 am
Re: Part_Measure_Linear output in macro
I don't know of a way to get at those measurements except maybe by parsing the report view output after running the refresh command. You can do something like this in a script, but you don't get a document object:
functions available:
m.length() #distance as a float
m.delta() #distance as a vector, e.g. (0, 5, 0) if distance is 5 in the y direction
m.radius()
m.angle()
m.com() #center of mass
m.addReference3D()
m.hasReferences()
m.clear()
Code: Select all
import Measure
doc = FreeCAD.ActiveDocument if FreeCAD.ActiveDocument else FreeCAD.newDocument()
box = doc.addObject("Part::Box","Box")
doc.recompute()
m = Measure.Measurement()
m.addReference3D("Box", "Vertex1")
m.addReference3D("Box", "Vertex3")
m.length() #should give distance from vertex1 to vertex3
m.length() #distance as a float
m.delta() #distance as a vector, e.g. (0, 5, 0) if distance is 5 in the y direction
m.radius()
m.angle()
m.com() #center of mass
m.addReference3D()
m.hasReferences()
m.clear()
Re: Part_Measure_Linear output in macro
Thank you for your answer.
Can this method of yours be used to measure the shortest distance between two faces between array objects?
I also tried to parse the Report View text, I got as far as this to select QDockWidget for it but didn't know how to parse the actual text next:
Does anyone know how to parse the content of the dock next? I am proficient enough with python to handle the string manipulation required afterwards, just need to see how the text is parsed.
Can this method of yours be used to measure the shortest distance between two faces between array objects?
I also tried to parse the Report View text, I got as far as this to select QDockWidget for it but didn't know how to parse the actual text next:
Code: Select all
from PySide import QtGui
mw=FreeCADGui.getMainWindow()
dws=mw.findChildren(QtGui.QDockWidget)
for i in dws:
if i.objectName() == "Report view":
dw=i
break
Re: Part_Measure_Linear output in macro
I've made some progress, but haven't cracked it yet. Looks like I might have needed to go in deeper because I found that there was a QTextEditor child object in dw, from that I could apply a clear to it, which works and clears the text in the Report View. From the QT docs, the command for reading text is toPlainText, but when I try to use it, it passes blank. Oddly, if I pass my own Hello World! text to the text editor by using a setPlainText call and then use toPlainText, I do indeed get back my Hello World, but not the other text from the measurement. I think the report messages are of a different content type. Anyway, just thought I'd add this new point in case it helps come to the answer.
Code: Select all
from PySide import QtGui
mw=FreeCADGui.getMainWindow()
dws=mw.findChildren(QtGui.QDockWidget)
for i in dws:
if i.objectName() == "Report view":
dw=i
break
for j in dw.children():
if j.objectName() == "Report view":
te=j
break
te.clear()
Gui.Selection.addSelection('myDoc','Array','8.Face1',-173.317,86.1548,-59.1654)
Gui.Selection.addSelection('myDoc','Array','9.Face1',-160.528,27.0422,-53.0667)
Gui.runCommand('Part_Measure_Linear',0)
text=te.toPlainText()
print(text)
-
- Veteran
- Posts: 5513
- Joined: Thu Apr 05, 2018 1:53 am
Re: Part_Measure_Linear output in macro
Code: Select all
from PySide import QtGui
rv = Gui.getMainWindow().findChild(QtGui.QTextEdit,"Report view")
if rv:
print(f"text = {rv.toPlainText()}")
Re: Part_Measure_Linear output in macro
Thanks for the reply but the output for text was still blank like mineTheMarkster wrote: ↑Tue May 03, 2022 4:06 pmCode: Select all
from PySide import QtGui rv = Gui.getMainWindow().findChild(QtGui.QTextEdit,"Report view") if rv: print(f"text = {rv.toPlainText()}")
EDIT: Added snippet
- Attachments
-
- Capture.JPG (188.31 KiB) Viewed 1095 times
-
- Veteran
- Posts: 3182
- Joined: Thu Sep 24, 2020 10:31 pm
- Location: Hawaii
- Contact:
Re: Part_Measure_Linear output in macro
Code: Select all
rv = Gui.getMainWindow().findChild(QtGui.QTextEdit,"Report view")
Re: Part_Measure_Linear output in macro
Nope, that doesn't work, see attached screen shot. Once the variable is set, it doesn't change. I inserted some print statements to prove it. Thanks for the reply.edwilliams16 wrote: ↑Thu May 05, 2022 5:40 pm Screen Shot 2022-05-05 at 7.37.46 AM.png
You need to insert anotheron line 8. your rv refered to the cleared window.Code: Select all
rv = Gui.getMainWindow().findChild(QtGui.QTextEdit,"Report view")
-
- Veteran
- Posts: 3182
- Joined: Thu Sep 24, 2020 10:31 pm
- Location: Hawaii
- Contact:
Re: Part_Measure_Linear output in macro
It worked for me. EDIT: In fact it works for me without the change! I'm selecting the points via the Gui.
Re: Part_Measure_Linear output in macro
Damn, I was worried that it was something about my settings somewhere that was hiding it. I wonder what is set different on you instance (and I assume the same for the Markster before also.)edwilliams16 wrote: ↑Thu May 05, 2022 6:16 pm It worked for me. EDIT: In fact it works for me without the change! I'm selecting the points via the Gui.
EDIT: Oh wait, it's because you selected the points in the gui perhaps, I need to be able to select it in the macro, well maybe not, but ideally.