ADDED: Action to embed all 3d models referenced

Moves through the footprints and replaces external references with
internal embedded files

Fixes https://gitlab.com/kicad/code/kicad/-/issues/20847
This commit is contained in:
Seth Hillbrand
2025-08-10 20:19:32 -07:00
committed by Alex Shvartzkop
parent f6fb40f110
commit 20b7d49c81
4 changed files with 73 additions and 1 deletions
+6
View File
@@ -1073,6 +1073,12 @@ TOOL_ACTION PCB_ACTIONS::generateBOM( TOOL_ACTION_ARGS()
.Tooltip( _( "Create bill of materials from board" ) )
.Icon( BITMAPS::post_bom ) );
TOOL_ACTION PCB_ACTIONS::collect3DModels( TOOL_ACTION_ARGS()
.Name( "pcbnew.EditorControl.collect3DModels" )
.Scope( AS_GLOBAL )
.FriendlyName( _( "Collect And Embed 3D Models" ) )
.Tooltip( _( "Collect footprint 3D models and embed them into the board" ) ) );
// Track & via size control
TOOL_ACTION PCB_ACTIONS::trackWidthInc( TOOL_ACTION_ARGS()
.Name( "pcbnew.EditorControl.trackWidthInc" )
+1
View File
@@ -552,6 +552,7 @@ public:
static TOOL_ACTION inspectConstraints;
static TOOL_ACTION diffFootprint;
static TOOL_ACTION showFootprintAssociations;
static TOOL_ACTION collect3DModels;
// Appearance controls
static TOOL_ACTION clearHighlight; // Turns off highlight and resets previous highlight
+65 -1
View File
@@ -51,7 +51,12 @@
#include <pcb_textbox.h>
#include <pcb_track.h>
#include <pcb_generator.h>
#include <project_pcb.h>
#include <wildcards_and_files_ext.h>
#include <filename_resolver.h>
#include <3d_cache/3d_cache.h>
#include <embedded_files.h>
#include <wx/filename.h>
#include <zone.h>
#include <confirm.h>
#include <kidialog.h>
@@ -1999,6 +2004,64 @@ int PCB_CONTROL::FlipPcbView( const TOOL_EVENT& aEvent )
return 0;
}
int PCB_CONTROL::CollectAndEmbed3DModels( const TOOL_EVENT& aEvent )
{
BOARD* brd = board();
if( !brd )
return 0;
PROJECT& prj = m_frame->Prj();
S3D_CACHE* cache = PROJECT_PCB::Get3DCacheManager( &prj );
FILENAME_RESOLVER* resolver = cache ? cache->GetResolver() : nullptr;
wxString workingPath = prj.GetProjectPath();
std::vector<const EMBEDDED_FILES*> stack;
stack.push_back( brd->GetEmbeddedFiles() );
BOARD_COMMIT commit( m_frame );
int embeddedCount = 0;
for( FOOTPRINT* fp : brd->Footprints() )
{
bool fpModified = false;
for( FP_3DMODEL& model : fp->Models() )
{
if( model.m_Filename.StartsWith( FILEEXT::KiCadUriPrefix ) )
continue;
wxString fullPath =
resolver ? resolver->ResolvePath( model.m_Filename, workingPath, stack ) : model.m_Filename;
wxFileName fname( fullPath );
if( fname.Exists() )
{
if( EMBEDDED_FILES::EMBEDDED_FILE* file = brd->GetEmbeddedFiles()->AddFile( fname, false ) )
{
model.m_Filename = file->GetLink();
fpModified = true;
embeddedCount++;
}
}
}
if( fpModified )
commit.Modify( fp );
}
if( embeddedCount > 0 )
{
commit.Push( _( "Embed 3D Models" ) );
wxString msg = wxString::Format( _( "%d 3D model(s) successfully embedded." ), embeddedCount );
m_frame->GetInfoBar()->ShowMessageFor( msg, 5000 );
}
return 0;
}
// clang-format off
void PCB_CONTROL::setTransitions()
{
@@ -2080,7 +2143,8 @@ void PCB_CONTROL::setTransitions()
Go( &PCB_CONTROL::SnapModeFeedback, PCB_EVENTS::SnappingModeChangedByKeyEvent() );
// Miscellaneous
Go( &PCB_CONTROL::InteractiveDelete, ACTIONS::deleteTool.MakeEvent() );
Go( &PCB_CONTROL::InteractiveDelete, ACTIONS::deleteTool.MakeEvent() );
Go( &PCB_CONTROL::CollectAndEmbed3DModels, PCB_ACTIONS::collect3DModels.MakeEvent() );
// Append control
Go( &PCB_CONTROL::AppendBoardFromFile, PCB_ACTIONS::appendBoard.MakeEvent() );
+1
View File
@@ -103,6 +103,7 @@ public:
int AppendBoardFromFile( const TOOL_EVENT& aEvent );
int AppendBoard( PCB_IO& pi, wxString& fileName );
int UpdateMessagePanel( const TOOL_EVENT& aEvent );
int CollectAndEmbed3DModels( const TOOL_EVENT& aEvent );
int FlipPcbView( const TOOL_EVENT& aEvent );