Skip to content

Commit 38e6b01

Browse files
committed
fix screenshot generation with Freecad O.21.2 and Wayland by setting prefs in code and launching weston, do not use singleInstance on dev machine
1 parent b1b4360 commit 38e6b01

File tree

1 file changed

+71
-49
lines changed

1 file changed

+71
-49
lines changed

grails-app/services/plm/PlmFreeCadUiService.groovy

Lines changed: 71 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ class PlmFreeCadUiService implements WebAttributes {
554554
return new File(filePath)
555555
else {
556556
try {
557-
createPreview(part)
557+
createPreview(part, filePath)
558558
} catch (Throwable t) {
559559
log.error(t.message)
560560
t.printStackTrace()
@@ -563,10 +563,72 @@ class PlmFreeCadUiService implements WebAttributes {
563563
return new File(filePath)
564564
}
565565

566-
private void createPreview(PlmFreeCadPart part) {
566+
private void createPreview(PlmFreeCadPart part, String filePath) {
567567
def zipFile = zipPart(part)
568-
String filePath = previewPath + '/' + part.plmContentShaOne + '.bmp'
569568
if (new File(filePath).exists()) return
569+
synchronized (singleton) {
570+
if (new File('/tmp/model').exists()) FileUtils.forceDelete(new File('/tmp/model'))
571+
"unzip ${zipFile.path} -d /tmp/model".execute()
572+
String conv = """\
573+
import sys, os
574+
575+
step = "/tmp/model/${partFileName(part).replace("\"", "'")}"
576+
webp = '${filePath}'
577+
578+
if (os.path.isfile(webp)):
579+
print("File exists, exiting ...")
580+
else:
581+
d = App.openDocument(step)
582+
FreeCADGui.ActiveDocument.ActiveView.setAnimationEnabled(False)
583+
print("Document Opened...")
584+
print("FreeCADGui.ActiveDocument.ActiveView " + str(FreeCADGui.ActiveDocument.ActiveView))
585+
FreeCADGui.ActiveDocument.ActiveView.viewIsometric()
586+
Gui.SendMsgToActiveView("OrthographicCamera")
587+
Gui.SendMsgToActiveView("ViewAxo")
588+
Gui.SendMsgToActiveView("ViewFit")
589+
print("Next Save Image ...")
590+
App.ParamGet("User parameter:BaseApp/Preferences/View").SetString("SavePicture", "FramebufferObject")
591+
FreeCADGui.ActiveDocument.ActiveView.saveImage(webp, 1448, 1760, 'Transparent')
592+
print("Saved, exiting...")
593+
594+
Gui.runCommand('Std_CloseAllWindows',0)
595+
Gui.runCommand('Std_Quit',0)
596+
""".stripIndent()
597+
Path convPath = Files.createTempFile("FreeCAD-Script", ".py")
598+
File convFile = convPath.toFile()
599+
convFile.append(conv)
600+
String cmd
601+
Process pWeston
602+
if (useWeston) {
603+
String pWestonCmd = "/usr/bin/weston --no-config --socket=wl-freecad --backend=headless"
604+
log.info "$pWestonCmd"
605+
pWeston = pWestonCmd.execute()
606+
cmd = "env WAYLAND_DISPLAY=wl-freecad ${freecadPath} ${singleInstance?'--single-instance':''} ${convFile.path}"
607+
} else {
608+
cmd = "${xvfbRun ? "/usr/bin/xvfb-run ":""}${freecadPath} ${singleInstance?'--single-instance':''} ${convFile.path}"
609+
}
610+
if (cmd) {
611+
log.info "$cmd"
612+
Process pFreecad = cmd.execute()
613+
int occ = 0
614+
println "Script:\n$conv"
615+
while (!new File(filePath).exists() && occ++ < 60) {
616+
sleep(1000)
617+
println "Wait $occ ${new File(filePath).exists()} ${filePath}"
618+
}
619+
println "Deleting ${convPath.toString()}"
620+
Files.deleteIfExists(convPath)
621+
}
622+
if (useWeston && pWeston) {
623+
println "killing weston"
624+
pWeston.waitForOrKill(1000)
625+
}
626+
}
627+
}
628+
629+
private void create3dPreview(PlmFreeCadPart part) {
630+
def zipFile = zipPart(part)
631+
if (new File("${glbPath + '/' + part.plmContentShaOne + '.glb'}").exists()) return
570632
synchronized (singleton) {
571633
if (new File('/tmp/model').exists()) FileUtils.forceDelete(new File('/tmp/model'))
572634
"unzip ${zipFile.path} -d /tmp/model".execute()
@@ -575,49 +637,15 @@ class PlmFreeCadUiService implements WebAttributes {
575637
import ImportGui
576638
from PySide import QtGui, QtCore
577639
578-
class MyFilter (QtCore.QObject):
579-
def eventFilter(self, obj, ev):
580-
if issubclass(type(ev), QtGui.QCloseEvent):
581-
return True
582-
return False
583-
584-
f = MyFilter()
585640
step = "/tmp/model/${partFileName(part).replace("\"", "'")}"
586-
webp = '${filePath}'
587641
glb = '${glbPath + '/' + part.plmContentShaOne + ".glb"}'
588642
589643
d = App.openDocument(step)
590644
mw=FreeCADGui.getMainWindow()
591645
mdi=mw.findChildren(QtGui.QMdiSubWindow)
592-
593-
#f=MyFilter()
594-
#for imdi in iter(mdi):
595-
# imdi.installEventFilter(f)
596-
646+
597647
ImportGui.export(FreeCAD.ActiveDocument.RootObjects, glb)
598648
599-
sw = Gui.getMainWindow().centralWidget().activeSubWindow()
600-
601-
count = 5
602-
603-
while count > 0 and None != sw and None != Gui.ActiveDocument and str(FreeCADGui.ActiveDocument.ActiveView) != 'View3DInventor':
604-
print('AUO33 ' + str(sw) + ' ' + str(FreeCADGui.ActiveDocument.ActiveView))
605-
sw.close()
606-
sw = Gui.getMainWindow().centralWidget().activeSubWindow()
607-
sw.installEventFilter(f)
608-
count -= 1
609-
610-
print('AUO' + str(FreeCADGui.ActiveDocument.ActiveView))
611-
FreeCADGui.ActiveDocument.ActiveView.viewIsometric()
612-
Gui.SendMsgToActiveView("ViewFit")
613-
FreeCADGui.ActiveDocument.ActiveView.saveImage(webp, 480, 300, 'Current')
614-
615-
mw=FreeCADGui.getMainWindow()
616-
mdi=mw.findChildren(QtGui.QMdiSubWindow)
617-
for imdi in iter(mdi):
618-
imdi.installEventFilter(f)
619-
# imdi.deleteLater()
620-
621649
App.closeDocument(d.Name)
622650
623651
Gui.runCommand('Std_CloseAllWindows',0)
@@ -639,24 +667,18 @@ class PlmFreeCadUiService implements WebAttributes {
639667
if (cmd) {
640668
log.info "$cmd"
641669
Process pFreecad = cmd.execute()
642-
int occ = 0
643670
println "Script:\n$conv"
644-
while (!new File(filePath).exists() && occ++ < 60) {
645-
sleep(1000)
646-
println "Wait $occ ${new File(filePath).exists()} ${filePath}"
647-
}
648671
println "Deleting ${convPath.toString()}"
649672
Files.deleteIfExists(convPath)
650673
}
651674
if (useWeston && pWeston) {
652-
println "Deleting ${convPath.toString()} & killing weston"
675+
println "killing weston"
653676
pWeston.waitForOrKill(1000)
654677
}
655-
if (new File(filePath).exists()) {
656-
"/usr/bin/convert ${filePath} ${previewPath + '/' + part.plmContentShaOne + '.webp'}".execute()
657-
} else {
658-
log.error "No BMP file preview ..."
659-
}
678+
660679
}
680+
681+
682+
661683
}
662684
}

0 commit comments

Comments
 (0)