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
parent c4d78d2bce
commit 02e9dd697b
4 changed files with 72 additions and 1 deletions

View File

@ -1284,6 +1284,12 @@ TOOL_ACTION PCB_ACTIONS::exportHyperlynx( TOOL_ACTION_ARGS()
.FriendlyName( _( "Hyperlynx..." ) ) .FriendlyName( _( "Hyperlynx..." ) )
.Icon( BITMAPS::export_step ) ); .Icon( BITMAPS::export_step ) );
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 // Track & via size control
TOOL_ACTION PCB_ACTIONS::trackWidthInc( TOOL_ACTION_ARGS() TOOL_ACTION PCB_ACTIONS::trackWidthInc( TOOL_ACTION_ARGS()

View File

@ -559,6 +559,7 @@ public:
static TOOL_ACTION inspectConstraints; static TOOL_ACTION inspectConstraints;
static TOOL_ACTION diffFootprint; static TOOL_ACTION diffFootprint;
static TOOL_ACTION showFootprintAssociations; static TOOL_ACTION showFootprintAssociations;
static TOOL_ACTION collect3DModels;
// Appearance controls // Appearance controls
static TOOL_ACTION clearHighlight; // Turns off highlight and resets previous highlight static TOOL_ACTION clearHighlight; // Turns off highlight and resets previous highlight

View File

@ -56,6 +56,10 @@
#include <pcb_generator.h> #include <pcb_generator.h>
#include <project_pcb.h> #include <project_pcb.h>
#include <wildcards_and_files_ext.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 <zone.h>
#include <confirm.h> #include <confirm.h>
#include <kidialog.h> #include <kidialog.h>
@ -2567,6 +2571,64 @@ int PCB_CONTROL::RehatchShapes( const TOOL_EVENT& aEvent )
} }
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;
boardModified = 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 // clang-format off
void PCB_CONTROL::setTransitions() void PCB_CONTROL::setTransitions()
{ {
@ -2656,6 +2718,7 @@ void PCB_CONTROL::setTransitions()
// Miscellaneous // 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 // Append control
Go( &PCB_CONTROL::AppendDesignBlock, PCB_ACTIONS::placeDesignBlock.MakeEvent() ); Go( &PCB_CONTROL::AppendDesignBlock, PCB_ACTIONS::placeDesignBlock.MakeEvent() );

View File

@ -113,6 +113,7 @@ public:
int UpdateMessagePanel( const TOOL_EVENT& aEvent ); int UpdateMessagePanel( const TOOL_EVENT& aEvent );
int PlaceCharacteristics( const TOOL_EVENT& aEvent ); int PlaceCharacteristics( const TOOL_EVENT& aEvent );
int PlaceStackup( const TOOL_EVENT& aEvent ); int PlaceStackup( const TOOL_EVENT& aEvent );
int CollectAndEmbed3DModels( const TOOL_EVENT& aEvent );
int FlipPcbView( const TOOL_EVENT& aEvent ); int FlipPcbView( const TOOL_EVENT& aEvent );