KiCad PCB EDA Suite
Loading...
Searching...
No Matches
design_block.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <design_block.h>
21
22#include <wx/tokenzr.h>
23#include <api/api_utils.h>
24
25
26std::vector<SEARCH_TERM>& DESIGN_BLOCK::GetSearchTerms()
27{
28 m_searchTerms.clear();
29 m_searchTerms.reserve( 6 );
30
31 m_searchTerms.emplace_back( SEARCH_TERM( GetLibNickname(), 4 ) );
32 m_searchTerms.emplace_back( SEARCH_TERM( GetName(), 8, true ) );
33 m_searchTerms.emplace_back( SEARCH_TERM( GetLIB_ID().Format(), 16, true ) );
34
35 wxStringTokenizer keywordTokenizer( GetKeywords(), wxS( " " ), wxTOKEN_STRTOK );
36
37 while( keywordTokenizer.HasMoreTokens() )
38 m_searchTerms.emplace_back( SEARCH_TERM( keywordTokenizer.GetNextToken(), 4 ) );
39
40 // Also include keywords as one long string, just in case
41 m_searchTerms.emplace_back( SEARCH_TERM( GetKeywords(), 1 ) );
42 m_searchTerms.emplace_back( SEARCH_TERM( GetDesc(), 1 ) );
43
44 return m_searchTerms;
45}
46
47
48void DESIGN_BLOCK::Serialize( kiapi::common::types::DesignBlock& aDesignBlock ) const
49{
50 using namespace kiapi::common;
51 using namespace kiapi::common::types;
52
53 DesignBlock& def = aDesignBlock;
54 def.Clear();
55 PackLibId( def.mutable_id(), m_lib_id );
56
57 def.set_schematic_filename( m_schematicFile.ToUTF8() );
58 def.set_board_filename( m_boardFile.ToUTF8() );
59 def.set_lib_description( m_libDescription.ToUTF8() );
60 def.set_keywords( m_keywords.ToUTF8() );
61
62 for( const auto& [name, value] : m_fields )
63 {
64 types::CustomProperty* field = def.mutable_fields()->Add();
65 field->set_key( name.ToUTF8() );
66 field->set_value( value.ToUTF8() );
67 }
68}
69
70
71void DESIGN_BLOCK::Serialize( google::protobuf::Any &aContainer ) const
72{
73 kiapi::common::types::DesignBlock designBlock;
74 Serialize( designBlock );
75
76 if( !aContainer.PackFrom( designBlock ) )
77 wxFAIL_MSG( wxT( "Failed to pack DESIGN_BLOCK" ) );
78}
79
80
81bool DESIGN_BLOCK::Deserialize( const kiapi::common::types::DesignBlock& aDesignBlock )
82{
83 using namespace kiapi::common;
84 using namespace kiapi::common::types;
85
86 const DesignBlock& def = aDesignBlock;
87
88 m_lib_id = UnpackLibId( def.id() );
89
90 m_schematicFile = def.schematic_filename();
91 m_boardFile = def.board_filename();
92 m_libDescription = def.lib_description();
93 m_keywords = def.keywords();
94
95 m_fields.clear();
96
97 for( const types::CustomProperty& prop : def.fields() )
98 m_fields[wxString::FromUTF8( prop.key() )] = wxString::FromUTF8( prop.value() );
99
100 return true;
101}
102
103
104bool DESIGN_BLOCK::Deserialize( const google::protobuf::Any& aContainer )
105{
106 kiapi::common::types::DesignBlock designBlock;
107
108 if( !aContainer.UnpackTo( &designBlock ) )
109 return false;
110
111 return Deserialize( designBlock );
112}
const char * name
wxString m_schematicFile
File name and path for schematic file.
wxString GetLibNickname() const override
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
std::vector< SEARCH_TERM > & GetSearchTerms() override
wxString GetDesc() override
LIB_ID GetLIB_ID() const override
const wxString & GetKeywords() const
wxString m_keywords
Search keywords to find design block in library.
LIB_ID m_lib_id
wxString m_libDescription
File name and path for documentation file.
std::vector< SEARCH_TERM > m_searchTerms
nlohmann::ordered_map< wxString, wxString > m_fields
wxString m_boardFile
File name and path for board file.
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
wxString GetName() const override
KICOMMON_API void PackLibId(types::LibraryIdentifier *aOutput, const LIB_ID &aId)
KICOMMON_API LIB_ID UnpackLibId(const types::LibraryIdentifier &aId)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition ptree.cpp:194
A structure for storing weighted search terms.