Part_Measure_Linear output in macro

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
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!
Nuvonic
Posts: 12
Joined: Wed Apr 27, 2022 11:30 am

Part_Measure_Linear output in macro

Post by Nuvonic »

Hello

How does one refer to the result/output of a Part_Measure_Linear command in a macro?

Thank you in advance,
Nuvonic
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: Part_Measure_Linear output in macro

Post by TheMarkster »

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:

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
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()
Nuvonic
Posts: 12
Joined: Wed Apr 27, 2022 11:30 am

Re: Part_Measure_Linear output in macro

Post by Nuvonic »

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:

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
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.
Nuvonic
Posts: 12
Joined: Wed Apr 27, 2022 11:30 am

Re: Part_Measure_Linear output in macro

Post by Nuvonic »

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)
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: Part_Measure_Linear output in macro

Post by TheMarkster »

Code: Select all

from PySide import QtGui
rv = Gui.getMainWindow().findChild(QtGui.QTextEdit,"Report view")
if rv:
    print(f"text = {rv.toPlainText()}")
Nuvonic
Posts: 12
Joined: Wed Apr 27, 2022 11:30 am

Re: Part_Measure_Linear output in macro

Post by Nuvonic »

TheMarkster wrote: Tue May 03, 2022 4:06 pm

Code: Select all

from PySide import QtGui
rv = Gui.getMainWindow().findChild(QtGui.QTextEdit,"Report view")
if rv:
    print(f"text = {rv.toPlainText()}")
Thanks for the reply but the output for text was still blank like mine :(
Image

EDIT: Added snippet
Attachments
Capture.JPG
Capture.JPG (188.31 KiB) Viewed 1095 times
edwilliams16
Veteran
Posts: 3182
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Part_Measure_Linear output in macro

Post by edwilliams16 »

Screen Shot 2022-05-05 at 7.37.46 AM.png
Screen Shot 2022-05-05 at 7.37.46 AM.png (174.76 KiB) Viewed 977 times
You need to insert another

Code: Select all

rv = Gui.getMainWindow().findChild(QtGui.QTextEdit,"Report view")
on line 8. your rv refered to the cleared window.
Nuvonic
Posts: 12
Joined: Wed Apr 27, 2022 11:30 am

Re: Part_Measure_Linear output in macro

Post by Nuvonic »

edwilliams16 wrote: Thu May 05, 2022 5:40 pm Screen Shot 2022-05-05 at 7.37.46 AM.png

You need to insert another

Code: Select all

rv = Gui.getMainWindow().findChild(QtGui.QTextEdit,"Report view")
on line 8. your rv refered to the cleared window.
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.
Capture2.JPG
Capture2.JPG (170.51 KiB) Viewed 955 times
edwilliams16
Veteran
Posts: 3182
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Part_Measure_Linear output in macro

Post by edwilliams16 »

It worked for me. EDIT: In fact it works for me without the change! I'm selecting the points via the Gui.
Nuvonic
Posts: 12
Joined: Wed Apr 27, 2022 11:30 am

Re: Part_Measure_Linear output in macro

Post by Nuvonic »

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.
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.)

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.
Post Reply