@@ -187,6 +187,39 @@ QByteArray loadFCStdThumbnail(const std::string& pathToFCStdFile)
187187 return {};
188188}
189189
190+ // / Attempt to generate a thumbnail image from a file using f3d or load from cache
191+ // / \returns The image bytes, or an empty QByteArray (if thumbnail generation fails)
192+ QByteArray getF3dThumbnail (const std::string& pathToFile)
193+ {
194+ QString thumbnailPath = getUniquePNG (pathToFile);
195+ if (!useCachedPNG (thumbnailPath.toStdString (), pathToFile)) {
196+ ParameterGrp::handle hGrp = App::GetApplication ().GetParameterGroupByPath (
197+ " User parameter:BaseApp/Preferences/Mod/Start" );
198+ auto f3d = QString::fromUtf8 (hGrp->GetASCII (" f3d" , " f3d" ).c_str ());
199+ const int resolution = 128 ;
200+ QStringList args;
201+ args << QLatin1String (" --config=thumbnail" ) << QLatin1String (" --load-plugins=occt" )
202+ << QLatin1String (" --verbose=quiet" ) << QLatin1String (" --output=" ) + thumbnailPath
203+ << QLatin1String (" --resolution=" ) + QString::number (resolution) + QLatin1String (" ," )
204+ + QString::number (resolution)
205+ << QString::fromStdString (pathToFile);
206+
207+ QProcess process;
208+ process.start (f3d, args);
209+ process.waitForFinished ();
210+ if (process.exitCode () != 0 ) {
211+ return {};
212+ }
213+ }
214+
215+ QFile thumbnailFile (thumbnailPath);
216+ if (thumbnailFile.exists ()) {
217+ thumbnailFile.open (QIODevice::OpenModeFlag::ReadOnly);
218+ return thumbnailFile.readAll ();
219+ }
220+ return {};
221+ }
222+
190223FileStats getFileInfo (const std::string& path)
191224{
192225 FileStats result;
@@ -296,31 +329,9 @@ void DisplayedFilesModel::addFile(const QString& filePath)
296329 }
297330 }
298331 else {
299- // If it is not a FreeCAD file, generate thumbnail using F3D
300- QString thumbnailPath = getUniquePNG (filePath.toStdString ());
301-
302- auto f3d = QString::fromLatin1 (" f3d" );
303- QStringList args;
304- args << QLatin1String (" --config=thumbnail" ) << QLatin1String (" --load-plugins=occt" )
305- << QLatin1String (" --verbose=quiet" ) << QLatin1String (" --output=" ) + thumbnailPath
306- << QLatin1String (" --resolution=" ) + QString::number (128 ) + QLatin1String (" ," )
307- + QString::number (128 )
308- << filePath;
309-
310- // Run the f3d command to generate the thumbnail
311- QProcess process;
312- process.start (f3d, args);
313- process.waitForFinished ();
314- int result = process.exitCode ();
315-
316- // Check if the thumbnail was generated successfully and exists
317- if (result == 0 && QFile::exists (thumbnailPath)) {
318- QFile thumbnailFile (thumbnailPath);
319- if (thumbnailFile.open (QIODevice::ReadOnly)) {
320- QByteArray thumbnailData = thumbnailFile.readAll ();
321- thumbnailFile.close ();
322- _imageCache.insert (filePath, thumbnailData);
323- }
332+ auto thumbnail = getF3dThumbnail (filePath.toStdString ());
333+ if (!thumbnail.isEmpty ()) {
334+ _imageCache.insert (filePath, thumbnail);
324335 }
325336 }
326337}
0 commit comments