OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
Design.cpp
Go to the documentation of this file.
1#include "Design.h"
2#include "Design.h"
3#include "Package.h"
4#include "Logger.h"
5#include "../enums.h"
6#include "Part.h"
8#include <cstdint>
9#include <memory>
10#include "Net.h"
11#include "../FileModel/Design/FileArchive.h"
12#include "../FileModel/Design/EdaDataFile.h"
13#include "../Text/Utf8Sanitizer.h"
14
15
17{
19 {
20 }
21
23 {
24 m_components.clear();
25 m_componentsByName.clear();
26 m_nets.clear();
27 m_netsByName.clear();
28 m_packages.clear();
29 m_packagesByName.clear();
30 m_parts.clear();
31 m_partsByName.clear();
32 }
33
34 const std::string& Design::GetName() const
35 {
36 return m_name;
37 }
38
39 const std::string& Design::GetProductModel() const
40 {
41 return m_productModel;
42 }
43
45 {
46 return m_nets;
47 }
48
50 {
51 return m_netsByName;
52 }
53
55 {
56 return m_packages;
57 }
58
60 {
61 return m_packagesByName;
62 }
63
65 {
66 return m_components;
67 }
68
70 {
71 return m_componentsByName;
72 }
73
74 std::shared_ptr<Component> Design::GetComponent(const std::string& refDes) const
75 {
76 auto findIt = m_componentsByName.find(refDes);
77 if (findIt != m_componentsByName.end())
78 {
79 return findIt->second;
80 }
81
82 return nullptr;
83 }
84
86 {
87 return m_parts;
88 }
89
91 {
92 return m_partsByName;
93 }
94
95 std::shared_ptr<Net> Design::GetNet(const std::string& name) const
96 {
97 auto findIt = m_netsByName.find(name);
98 if (findIt != m_netsByName.end())
99 {
100 return findIt->second;
101 }
102 return nullptr;
103 }
104
105 std::shared_ptr<Net> Design::GetNoneNet() const
106 {
107 return GetNet(NONE_NET_NAME);
108 }
109
110 bool Design::Build(std::string path)
111 {
112 auto pFileModel = std::make_shared<FileModel::Design::FileArchive>(path);
113 if (pFileModel->ParseFileModel())
114 {
115 return Build(pFileModel);
116 }
117 return false;
118 }
119
120 bool Design::Build(std::shared_ptr<FileModel::Design::FileArchive> pFileModel)
121 {
122 if (pFileModel == nullptr) return false;
123
124 m_pFileModel = pFileModel;
125
126 //m_productModel = m_pFileModel->GetProductName();
127 //m_name = m_pFileModel->GetProductName();
128
129 // atomic elements
130 if (! BuildNets()) return false;
131 if (! BuildPackages()) return false;
132 if (! BuildAllParts()) return false;
133 if (! BuildAllComponents()) return false;
134
135 // built from relationships between atomic elements
136 if (! BuildPlacementsFromComponentsFiles()) return false;
137
138 // already built from BuildPlacements()
139 //if (! BuildNoneNet()) return false;
140 //if (! BreakSinglePinNets()) return false;
141
142 if (CLIP_FILEMODEL_AFTER_BUILD)
143 {
145 }
146
147 return true;
148 }
149
150 std::shared_ptr<FileModel::Design::FileArchive> Design::GetFileModel() const
151 {
152 return m_pFileModel;
153 }
154
156 {
157 m_pFileModel = nullptr;
158 }
159
160 std::unique_ptr<Odb::Lib::Protobuf::ProductModel::Design> Design::to_protobuf() const
161 {
162 // Full flavor (with normalized lists) — REST (to_json), to_pbstring,
163 // to_stream, and round-trip serialization tests all route through this
164 // override and must continue to see the complete payload.
165 return to_protobuf(/*includeNormalizedLists=*/true);
166 }
167
168 std::unique_ptr<Odb::Lib::Protobuf::ProductModel::Design> Design::to_protobuf(bool includeNormalizedLists) const
169 {
170 auto pDesignMsg = std::make_unique<Odb::Lib::Protobuf::ProductModel::Design>();
171 pDesignMsg->set_name(Odb::Lib::Text::ToUtf8(m_name));
172 pDesignMsg->set_productmodel(Odb::Lib::Text::ToUtf8(m_productModel));
173
174 if (m_pFileModel != nullptr)
175 {
176 pDesignMsg->mutable_filemodel()->CopyFrom(*m_pFileModel->to_protobuf());
177 }
178
179 if (includeNormalizedLists)
180 {
181 for (const auto& pNet : m_nets)
182 {
183 pDesignMsg->add_nets()->CopyFrom(*pNet->to_protobuf());
184 }
185
186 FillProtobufMapWithSanitizedKeys(*pDesignMsg->mutable_netsbyname(), m_netsByName);
187
188 for (const auto& pPackage : m_packages)
189 {
190 pDesignMsg->add_packages()->CopyFrom(*pPackage->to_protobuf());
191 }
192
193 FillProtobufMapWithSanitizedKeys(*pDesignMsg->mutable_packagesbyname(), m_packagesByName);
194
195 for (const auto& pComponent : m_components)
196 {
197 pDesignMsg->add_components()->CopyFrom(*pComponent->to_protobuf());
198 }
199
200 FillProtobufMapWithSanitizedKeys(*pDesignMsg->mutable_componentsbyname(), m_componentsByName);
201
202 for (const auto& pPart : m_parts)
203 {
204 pDesignMsg->add_parts()->CopyFrom(*pPart->to_protobuf());
205 }
206
207 FillProtobufMapWithSanitizedKeys(*pDesignMsg->mutable_partsbyname(), m_partsByName);
208 }
209
210 // Connectivity is always-on (decision D4): derived here
211 // unconditionally — outside the includeNormalizedLists gate — so every
212 // flavour carries it and no client ever has to ask for it.
213 FillConnectivityMessage(pDesignMsg->mutable_connectivity());
214
215#ifndef NDEBUG
216 // Debug-only assertion: verify all string fields are valid UTF-8 before serialization
218#endif
219
220 return pDesignMsg;
221 }
222
223 void Design::FillConnectivityMessage(Protobuf::ProductModel::Connectivity* pConnectivityMsg) const
224 {
225 // Monolithic path: one message, all four sections. The section
226 // fillers are the single implementation — the chunked GetDesignStream
227 // path (M6) calls them with batch-sized Connectivity messages so both
228 // paths emit byte-identical entries by construction.
229 const auto index = BuildConnectivityIndex();
230 FillConnectivityNetEntries(*pConnectivityMsg);
231 FillConnectivityPackagePins(*pConnectivityMsg);
232 FillConnectivityNetMembers(index, *pConnectivityMsg);
233 FillConnectivityPlacedComponents(index, *pConnectivityMsg);
234 }
235
237 {
238 ConnectivityIndex index;
239
240 // $NONE$ ordinal (the roster loop in FillConnectivityNetEntries
241 // detects it too; the index needs it for the edge filter below).
242 for (const auto& pNet : m_nets)
243 {
244 if (!index.hasNoneNet && pNet->GetName() == NONE_NET_NAME)
245 {
246 index.noneNetOrdinal = pNet->GetIndex();
247 index.hasNoneNet = true;
248 }
249 }
250
251 // The resolved edges the product model already holds. Grouped by
252 // refDes then pin number; on a (refDes, pin) that somehow carries two
253 // edges the last one wins (no fixture design exhibits this).
254 for (const auto& pNet : m_nets)
255 {
256 // $NONE$ membership is a labeling convention, not a connection
257 // (decision DP4): those pins emit UNCONNECTED and join no
258 // netMembers entry.
259 if (index.hasNoneNet && pNet->GetIndex() == index.noneNetOrdinal) continue;
260
261 for (const auto& pPinConnection : pNet->GetPinConnections())
262 {
263 const auto& pComponent = pPinConnection->GetComponent();
264 const auto& pPin = pPinConnection->GetPin();
265 if (pComponent == nullptr || pPin == nullptr) continue;
266
267 index.edgesByRefDes[pComponent->GetRefDes()][pPin->GetIndex()] =
268 ConnectivityIndex::Edge{ pNet->GetIndex(), pPinConnection->GetSubnetNumber() };
269 }
270 }
271
272 // Net members: the same CONNECTED edges, inverted, for the net tab.
273 // Map iteration order gives net-ordinal-then-refDes determinism and
274 // pin numbers ascend within each member.
275 for (const auto& refDesAndPins : index.edgesByRefDes)
276 {
277 for (const auto& pinAndEdge : refDesAndPins.second)
278 {
279 index.membersByNet[pinAndEdge.second.net][refDesAndPins.first].push_back(pinAndEdge.first);
280 }
281 }
282 return index;
283 }
284
285 void Design::FillConnectivityNetEntries(Protobuf::ProductModel::Connectivity& connectivityMsg) const
286 {
287 for (std::size_t netIndex = 0; netIndex < m_nets.size(); ++netIndex)
288 {
289 AddConnectivityNetEntry(netIndex, connectivityMsg);
290 }
291 }
292
293 void Design::FillConnectivityPackagePins(Protobuf::ProductModel::Connectivity& connectivityMsg) const
294 {
295 for (std::size_t packageIndex = 0; packageIndex < m_packages.size(); ++packageIndex)
296 {
297 AddConnectivityPackagePinEntry(packageIndex, connectivityMsg);
298 }
299 }
300
301 void Design::FillConnectivityNetMembers(const ConnectivityIndex& index, Protobuf::ProductModel::Connectivity& connectivityMsg) const
302 {
303 for (auto membersIt = index.membersByNet.begin(); membersIt != index.membersByNet.end(); ++membersIt)
304 {
305 AddConnectivityNetMemberEntry(index, membersIt, connectivityMsg);
306 }
307 }
308
309 void Design::FillConnectivityPlacedComponents(const ConnectivityIndex& index, Protobuf::ProductModel::Connectivity& connectivityMsg) const
310 {
311 for (std::size_t componentIndex = 0; componentIndex < m_components.size(); ++componentIndex)
312 {
313 AddConnectivityPlacedComponentEntry(index, componentIndex, connectivityMsg);
314 }
315 }
316
317 void Design::AddConnectivityNetEntry(std::size_t netIndex, Protobuf::ProductModel::Connectivity& connectivityMsg) const
318 {
319 // Net roster: dense — every eda-data NET record in sequence, $NONE$
320 // included — so `net` ordinals keep their spec identity (p.153: the
321 // ordinal IS the NET record sequence) and clients need no name
322 // conventions to interpret them.
323 const auto& pNet = m_nets[netIndex];
324 auto pNetEntryMsg = connectivityMsg.add_nets();
325 pNetEntryMsg->set_ordinal(pNet->GetIndex());
326 pNetEntryMsg->set_name(Odb::Lib::Text::ToUtf8(pNet->GetName()));
327 }
328
329 void Design::AddConnectivityPackagePinEntry(std::size_t packageIndex, Protobuf::ProductModel::Connectivity& connectivityMsg) const
330 {
331 // Package pin rosters — deduped by construction (one entry per
332 // package, not per component). Package::AddPin assigns sequential
333 // indices, so each roster is already pin-number ascending.
334 const auto& pPackage = m_packages[packageIndex];
335 auto pPackagePinsMsg = connectivityMsg.add_packages();
336 pPackagePinsMsg->set_package(Odb::Lib::Text::ToUtf8(pPackage->GetName()));
337 for (const auto& pPin : pPackage->GetPins())
338 {
339 auto pPackagePinMsg = pPackagePinsMsg->add_pins();
340 pPackagePinMsg->set_number(pPin->GetIndex());
341 pPackagePinMsg->set_name(Odb::Lib::Text::ToUtf8(pPin->GetName()));
342 }
343 }
344
345 void Design::AddConnectivityNetMemberEntry(const ConnectivityIndex& index, ConnectivityIndex::MembersByNetConstIt netMembersIt, Protobuf::ProductModel::Connectivity& connectivityMsg) const
346 {
347 auto pNetMembersMsg = connectivityMsg.add_netmembers();
348 pNetMembersMsg->set_net(netMembersIt->first);
349 for (const auto& refDesAndPins : netMembersIt->second)
350 {
351 auto pMemberMsg = pNetMembersMsg->add_members();
352 pMemberMsg->set_refdes(Odb::Lib::Text::ToUtf8(refDesAndPins.first));
353 for (const auto pinNumber : refDesAndPins.second)
354 {
355 pMemberMsg->add_pin(pinNumber);
356 }
357 }
358 }
359
360 void Design::AddConnectivityPlacedComponentEntry(const ConnectivityIndex& index, std::size_t componentIndex, Protobuf::ProductModel::Connectivity& connectivityMsg) const
361 {
362 using ConnectivityMsg = Protobuf::ProductModel::Connectivity;
363
364 // Placed components in package-roster order (decision DP3). A pin
365 // with no edge — net_num -1 (skipped at build time) or a roster pin
366 // the design never placed a toeprint on — is UNCONNECTED with no net
367 // and no subnets; a toeprintNumber never reaches a client.
368 const auto& pComponent = m_components[componentIndex];
369 auto pPlacedComponentMsg = connectivityMsg.add_components();
370 pPlacedComponentMsg->set_refdes(Odb::Lib::Text::ToUtf8(pComponent->GetRefDes()));
371 const auto id = pComponent->GetId();
372 if (id.has_value())
373 {
374 pPlacedComponentMsg->set_id(*id);
375 }
376 pPlacedComponentMsg->set_side(static_cast<Odb::Lib::Protobuf::BoardSide>(pComponent->GetSide()));
377
378 const auto& pPackage = pComponent->GetPackage();
379 if (pPackage == nullptr) return;
380 pPlacedComponentMsg->set_package(Odb::Lib::Text::ToUtf8(pPackage->GetName()));
381
382 const auto findEdgesIt = index.edgesByRefDes.find(pComponent->GetRefDes());
383 for (const auto& pPin : pPackage->GetPins())
384 {
385 auto pComponentPinMsg = pPlacedComponentMsg->add_pins();
386 pComponentPinMsg->set_number(pPin->GetIndex());
387 pComponentPinMsg->set_name(Odb::Lib::Text::ToUtf8(pPin->GetName()));
388
389 auto pConnectionMsg = pComponentPinMsg->mutable_connection();
390 if (findEdgesIt != index.edgesByRefDes.end())
391 {
392 const auto findEdgeIt = findEdgesIt->second.find(pPin->GetIndex());
393 if (findEdgeIt != findEdgesIt->second.end())
394 {
395 pConnectionMsg->set_kind(ConnectivityMsg::Connection::CONNECTED);
396 pConnectionMsg->set_net(findEdgeIt->second.net);
397 pConnectionMsg->add_subnets(findEdgeIt->second.subnet);
398 continue;
399 }
400 }
401 pConnectionMsg->set_kind(ConnectivityMsg::Connection::UNCONNECTED);
402 }
403 }
404
405 void Design::from_protobuf(const Odb::Lib::Protobuf::ProductModel::Design& message)
406 {
407 m_name = message.name();
408 m_productModel = message.productmodel();
409
410 m_pFileModel = std::make_shared<FileModel::Design::FileArchive>();
411 m_pFileModel->from_protobuf(message.filemodel());
412
413 for (const auto& pNetMsg : message.nets())
414 {
415 auto pNet = std::make_shared<Net>("", -1);
416 pNet->from_protobuf(pNetMsg);
417 m_nets.push_back(pNet);
418 }
419
420 for (const auto& kvNetMsg : message.netsbyname())
421 {
422 auto pNet = std::make_shared<Net>("", -1);
423 pNet->from_protobuf(kvNetMsg.second);
424 m_netsByName[kvNetMsg.first] = pNet;
425 }
426
427 for (const auto& pPackageMsg : message.packages())
428 {
429 auto pPackage = std::make_shared<Package>("", -1);
430 pPackage->from_protobuf(pPackageMsg);
431 m_packages.push_back(pPackage);
432 }
433
434 for (const auto& kvPackageMsg : message.packagesbyname())
435 {
436 auto pPackage = std::make_shared<Package>("", -1);
437 pPackage->from_protobuf(kvPackageMsg.second);
438 m_packagesByName[kvPackageMsg.first] = pPackage;
439 }
440
441 for (const auto& pComponentMsg : message.components())
442 {
443 auto pComponent = std::make_shared<Component>("", "", nullptr, -1, BoardSide::BsNone, nullptr);
444 pComponent->from_protobuf(pComponentMsg);
445 m_components.push_back(pComponent);
446 }
447
448 for (const auto& kvComponentMsg : message.componentsbyname())
449 {
450 auto pComponent = std::make_shared<Component>("", "", nullptr, -1, BoardSide::BsNone, nullptr);
451 pComponent->from_protobuf(kvComponentMsg.second);
452 m_componentsByName[kvComponentMsg.first] = pComponent;
453 }
454
455 for (const auto& pPartMsg : message.parts())
456 {
457 auto pPart = std::make_shared<Part>("");
458 pPart->from_protobuf(pPartMsg);
459 m_parts.push_back(pPart);
460 }
461
462 for (const auto& kvPartMsg : message.partsbyname())
463 {
464 auto pPart = std::make_shared<Part>("");
465 pPart->from_protobuf(kvPartMsg.second);
466 m_partsByName[kvPartMsg.first] = pPart;
467 }
468 }
469
470 std::uint64_t Design::EstimateWireSizeBytes(bool includeNormalizedLists) const
471 {
472 // Per-record wire-byte coefficients. These are deliberately rough
473 // upper-mid estimates, calibrated so that (a) the small CI designs
474 // (sample_design, rigidflex) stay far below the monolithic serve
475 // budget and (b) the two OOM designs (200-40628, 350-41017) land far
476 // above it; the dispatch decision only needs that separation, not an
477 // accurate size. A miss LOW costs a pod OOM (the exact failure this
478 // guard exists for); a miss HIGH costs only a streaming detour — so
479 // the DOMINANT BULK is counted with generous coefficients. Not every
480 // serialized term is counted per ride: ByName-map duplication of
481 // net/package/pin records and the per-record propertyrecords embedded
482 // in NetRecord/PackageRecord/FeatureGroupRecord are amortized into
483 // the per-record coefficients, and standard-fonts bulk rides the flat
484 // MISC allowance. DesignStreamTests
485 // (EstimateWireSizeBytes_StaysAboveHalfOfActualSerializedSize) pins
486 // estimate >= 0.5x actual on the CI corpus so a dropped term cannot
487 // pass CI silently.
488 // A FeatureRecord carries ~10 scalar fields plus occasional
489 // strings/contours (~128 B), a component rides three places
490 // (components file + product-model Component + connectivity
491 // PlacedComponent with per-pin entries, ~768 B), a net two-to-three
492 // places (~128 B), a package record with its pin roster ~256 B.
493 constexpr std::uint64_t FEATURE_RECORD_WIRE_BYTES = 128;
494 constexpr std::uint64_t COMPONENT_WIRE_BYTES = 768;
495 constexpr std::uint64_t NET_WIRE_BYTES = 128;
496 constexpr std::uint64_t PACKAGE_WIRE_BYTES = 256;
497 // Subnet feature-id: type+layer+feature varints (~8 B wire), riding
498 // twice (EdaDataFile netRecords + netRecordsByName map).
499 constexpr std::uint64_t FEATURE_ID_RECORD_WIRE_BYTES = 16;
500 // Netlist-file records: serial#+netName (net) / ~13 numeric fields
501 // (point); both ride twice (records + ByName map).
502 constexpr std::uint64_t NETLIST_NET_WIRE_BYTES = 64;
503 constexpr std::uint64_t NETLIST_POINT_WIRE_BYTES = 64;
504 // Attribute-string entries (attrlist files, eda layer/attribute/text
505 // tables, per-record attribute lookup tables): name + value strings.
506 constexpr std::uint64_t ATTRIBUTE_ENTRY_WIRE_BYTES = 64;
507 // EdaDataFile property + feature-group records (name + value refs).
508 constexpr std::uint64_t PROPERTY_RECORD_WIRE_BYTES = 64;
509 // Tools-file drill/tool records (type, diameter, plus params).
510 constexpr std::uint64_t TOOL_RECORD_WIRE_BYTES = 64;
511 // Normalized flavour: a Part entry (name + package ref) rides the
512 // parts vector and the partsByName map.
513 constexpr std::uint64_t PART_WIRE_BYTES = 128;
514 // Header-ish files (matrix, misc/info, step headers, standard fonts)
515 // hold bounded structural payloads, not per-record bulk: a flat
516 // allowance per design plus per step covers them with margin.
517 constexpr std::uint64_t MISC_DESIGN_ALLOWANCE_BYTES = 256 * 1024;
518 constexpr std::uint64_t MISC_STEP_ALLOWANCE_BYTES = 64 * 1024;
519
520 std::uint64_t featureCount = 0;
521 std::uint64_t componentCount = 0;
522 std::uint64_t netCount = 0;
523 std::uint64_t packageCount = 0;
524 std::uint64_t featureIdCount = 0;
525 std::uint64_t netlistNetCount = 0;
526 std::uint64_t netlistPointCount = 0;
527 std::uint64_t attributeEntryCount = 0;
528 std::uint64_t propertyRecordCount = 0;
529 std::uint64_t toolCount = 0;
530 std::uint64_t stepCount = 0;
531
532 if (m_pFileModel != nullptr)
533 {
534 attributeEntryCount += m_pFileModel->GetMiscAttrListFile().GetAttributes().size();
535
536 for (const auto& kvSymbolsDirectory : m_pFileModel->GetSymbolsDirectoriesByName())
537 {
538 const auto& pSymbolsDirectory = kvSymbolsDirectory.second;
539 featureCount += pSymbolsDirectory->GetFeaturesFile().GetFeatureRecords().size();
540 attributeEntryCount += pSymbolsDirectory->GetAttrListFile().GetAttributes().size();
541 }
542
543 for (const auto& kvStep : m_pFileModel->GetStepsByName())
544 {
545 const auto& pStep = kvStep.second;
546 ++stepCount;
547 featureCount += pStep->GetProfileFile().GetFeatureRecords().size();
548 attributeEntryCount += pStep->GetAttrListFile().GetAttributes().size();
549
550 const auto& edaDataFile = pStep->GetEdaDataFile();
551 netCount += edaDataFile.GetNetRecords().size();
552 packageCount += edaDataFile.GetPackageRecords().size();
553 attributeEntryCount += edaDataFile.GetLayerNames().size()
554 + edaDataFile.GetAttributeNames().size()
555 + edaDataFile.GetAttributeTextValues().size();
556 propertyRecordCount += edaDataFile.GetPropertyRecords().size()
557 + edaDataFile.GetFeatureGroupRecords().size();
558 for (const auto& pNetRecord : edaDataFile.GetNetRecords())
559 {
560 for (const auto& pSubnetRecord : pNetRecord->m_subnetRecords)
561 {
562 featureIdCount += pSubnetRecord->m_featureIdRecords.size();
563 }
564 attributeEntryCount += pNetRecord->GetAttributeLookupTable().size();
565 }
566 for (const auto& pPackageRecord : edaDataFile.GetPackageRecords())
567 {
568 attributeEntryCount += pPackageRecord->GetAttributeLookupTable().size();
569 }
570
571 for (const auto& kvNetlist : pStep->GetNetlistsByName())
572 {
573 netlistNetCount += kvNetlist.second->GetNetRecords().size();
574 netlistPointCount += kvNetlist.second->GetNetPointRecords().size();
575 }
576
577 for (const auto& kvLayer : pStep->GetLayersByName())
578 {
579 const auto& pLayer = kvLayer.second;
580 featureCount += pLayer->GetFeaturesFile().GetFeatureRecords().size();
581 componentCount += pLayer->GetComponentsFile().GetComponentRecords().size();
582 attributeEntryCount += pLayer->GetAttrListFile().GetAttributes().size();
583 toolCount += pLayer->GetToolsFile().GetTools().size();
584 }
585 }
586 }
587
588 auto estimate = featureCount * FEATURE_RECORD_WIRE_BYTES
589 + componentCount * COMPONENT_WIRE_BYTES
590 + netCount * NET_WIRE_BYTES
591 + packageCount * PACKAGE_WIRE_BYTES
592 + featureIdCount * FEATURE_ID_RECORD_WIRE_BYTES
593 + netlistNetCount * NETLIST_NET_WIRE_BYTES
594 + netlistPointCount * NETLIST_POINT_WIRE_BYTES
595 + attributeEntryCount * ATTRIBUTE_ENTRY_WIRE_BYTES
596 + propertyRecordCount * PROPERTY_RECORD_WIRE_BYTES
597 + toolCount * TOOL_RECORD_WIRE_BYTES
598 + MISC_DESIGN_ALLOWANCE_BYTES + stepCount * MISC_STEP_ALLOWANCE_BYTES;
599
600 if (includeNormalizedLists)
601 {
602 // The full flavour repeats nets/components/packages (vectors plus
603 // ByName maps) on top of the fileModel-derived payload, and adds
604 // the parts vector + partsByName map.
605 estimate += netCount * NET_WIRE_BYTES
606 + componentCount * COMPONENT_WIRE_BYTES
607 + packageCount * PACKAGE_WIRE_BYTES
608 + m_parts.size() * PART_WIRE_BYTES;
609 }
610
611 return estimate;
612 }
613
614 bool Design::BuildAllComponents()
615 {
616 if (m_pFileModel == nullptr) return false;
617 const auto& steps = m_pFileModel->GetStepsByName();
618 if (steps.empty()) return false;
619 auto& pStepDirectory = steps.begin()->second;
620
621 // top components layer
622 auto pTopComponentsFile = pStepDirectory->GetTopComponentsFile();
623 if (pTopComponentsFile == nullptr) return false;
624 if (! BuildComponents(pTopComponentsFile)) return false;
625
626 // bottom layer components
627 auto pBottomComponentsFile = pStepDirectory->GetBottomComponentsFile();
628 if (pBottomComponentsFile == nullptr) return false;
629 if (!BuildComponents(pBottomComponentsFile)) return false;
630
631 return true;
632 }
633
634 bool Design::BuildComponents(const Odb::Lib::FileModel::Design::ComponentsFile* pComponentsFile)
635 {
636 const auto& componentRecords = pComponentsFile->GetComponentRecords();
637 for (const auto& pComponentRecord : componentRecords)
638 {
639 auto& pPackage = m_packages[pComponentRecord->pkgRef];
640 auto index = pComponentRecord->index;
641 auto& pPart = m_partsByName[pComponentRecord->partName];
642 auto pComponent = std::make_shared<Component>(pComponentRecord->compName, pComponentRecord->partName, pPackage, index, pComponentsFile->GetSide(), pPart, pComponentRecord->id);
643
644 m_components.push_back(pComponent);
645 m_componentsByName[pComponent->GetRefDes()] = pComponent;
646 }
647
648 return true;
649 }
650
651 bool Design::BuildVias()
652 {
653 return false;
654 }
655
656 bool Design::BuildNets()
657 {
658 if (m_pFileModel == nullptr) return false;
659
660 const auto& steps = m_pFileModel->GetStepsByName();
661 if (steps.empty()) return false;
662
663 auto& pStepDirectory = steps.begin()->second;
664 const auto& edaData = pStepDirectory->GetEdaDataFile();
665 const auto& netRecords = edaData.GetNetRecords();
666
667 for (const auto& pNetRecord : netRecords)
668 {
669 auto pNet = std::make_shared<Net>(pNetRecord->name, pNetRecord->index);
670 m_nets.push_back(pNet);
671 m_netsByName[pNet->GetName()] = pNet;
672 }
673
674 return true;
675 }
676
677 bool Design::BuildPackages()
678 {
679 if (m_pFileModel == nullptr) return false;
680
681 const auto& steps = m_pFileModel->GetStepsByName();
682 if (steps.empty()) return false;
683
684 auto& pStepDirectory = steps.begin()->second;
685 const auto& edaData = pStepDirectory->GetEdaDataFile();
686 const auto& packageRecords = edaData.GetPackageRecords();
687
688 for (const auto& pPackageRecord : packageRecords)
689 {
690 auto index = pPackageRecord->index;
691 auto pPackage = std::make_shared<Package>(pPackageRecord->name, index);
692
693 for (const auto& pPinRecord : pPackageRecord->m_pinRecords)
694 {
695 // TODO: figure out how to handle size_t -> non-size_t conversion
696 pPackage->AddPin(pPinRecord->name);
697 }
698
699 m_packages.push_back(pPackage);
700 m_packagesByName[pPackage->GetName()] = pPackage;
701 }
702
703 return true;
704 }
705
706 bool Design::BuildAllParts()
707 {
708 if (m_pFileModel == nullptr) return false;
709
710 const auto& steps = m_pFileModel->GetStepsByName();
711 if (steps.empty()) return false;
712
713 auto& pStepDirectory = steps.begin()->second;
714
715 auto pTopComponentsFile = pStepDirectory->GetTopComponentsFile();
716 if (pTopComponentsFile == nullptr) return false;
717 if (! BuildParts(pTopComponentsFile)) return false;
718
719 auto pBottomComponentsFile = pStepDirectory->GetBottomComponentsFile();
720 if (pBottomComponentsFile == nullptr) return false;
721 if (! BuildParts(pBottomComponentsFile)) return false;
722
723 return true;
724 }
725
726 bool Design::BuildParts(const Odb::Lib::FileModel::Design::ComponentsFile* pComponentsFile)
727 {
728 const auto& componentRecords = pComponentsFile->GetComponentRecords();
729 for (const auto& pComponentRecord : componentRecords)
730 {
731 auto& partName = pComponentRecord->partName;
732 auto findIt = m_partsByName.find(partName);
733 if (findIt == m_partsByName.end())
734 {
735 auto pPart = std::make_shared<Part>(partName);
736 m_parts.push_back(pPart);
737 m_partsByName[partName] = pPart;
738 }
739 }
740
741 return true;
742 }
743
744 bool Design::BuildPlacementsFromComponentsFiles()
745 {
746 if (m_pFileModel == nullptr) return false;
747
748 const auto& steps = m_pFileModel->GetStepsByName();
749 if (steps.empty()) return false;
750
751 // "first" step when there are >1 steps
752 auto& pStepDirectory = steps.begin()->second;
753
754 auto pTopComponentsFile = pStepDirectory->GetTopComponentsFile();
755 if (pTopComponentsFile == nullptr) return false;
756 if (! BuildPlacementsFromComponentsFile(pTopComponentsFile)) return false;
757
758 auto pBottomComponentsFile = pStepDirectory->GetBottomComponentsFile();
759 if (pBottomComponentsFile == nullptr) return false;
760 if (!BuildPlacementsFromComponentsFile(pBottomComponentsFile)) return false;
761
762 return true;
763 }
764
765 bool Design::BuildPlacementsFromComponentsFile(const Odb::Lib::FileModel::Design::ComponentsFile* pComponentsFile)
766 {
767 const auto& componentRecords = pComponentsFile->GetComponentRecords();
768 for (const auto& pComponentRecord : componentRecords)
769 {
770 const auto& toeprintRecords = pComponentRecord->m_toeprintRecords;
771 for (const auto& pToeprintRecord : toeprintRecords)
772 {
773 auto& toeprintName = pToeprintRecord->name;
774 auto pinNumber = pToeprintRecord->pinNumber;
775 auto netNumber = pToeprintRecord->netNumber;
776 auto subnetNumber = pToeprintRecord->subnetNumber;
777 auto& refDes = pComponentRecord->compName;
778
779 // -1 means no connection for the component pin
780 if (netNumber != static_cast<unsigned int>(-1))
781 {
782 if (!CreatePinConnection(refDes, netNumber, pinNumber, toeprintName, subnetNumber)) return false;
783 }
784 }
785 }
786
787 return true;
788 }
789
790 bool Design::CreatePinConnection(const std::string& refDes, unsigned int netNumber, unsigned int pinNumber, const std::string& pinName, unsigned int subnetNumber)
791 {
792 if (netNumber < m_nets.size())
793 {
794 auto& pComponent = m_componentsByName[refDes];
795 if (pComponent == nullptr) return false;
796
797 auto pPin = pComponent->GetPackage()->GetPin(pinNumber);
798 if (pPin == nullptr) return false;
799
800 auto& pNet = m_nets[netNumber];
801 if (pNet == nullptr) return false;
802
803 if (!pNet->AddPinConnection(pComponent, pPin, subnetNumber)) return false;
804 return true;
805 }
806 else
807 {
808 logwarn("netNumber out of range: " + std::to_string(netNumber) + ", size = " + std::to_string(m_nets.size()));
809 }
810
811 return false;
812 }
813
814 bool Design::BuildNoneNet()
815 {
816 auto pStepDirectory = m_pFileModel->GetStepDirectory();
817 if (pStepDirectory == nullptr) return false;
818
819 const auto& edaData = pStepDirectory->GetEdaDataFile();
820 auto findIt = edaData.GetNetRecordsByName().find(NONE_NET_NAME);
821 if (findIt == edaData.GetNetRecordsByName().end()) return false;
822
823 auto& pNoneNet = findIt->second;
824 if (pNoneNet == nullptr) return false;
825
826 if (!CreateNetConnections(pNoneNet, pStepDirectory)) return false;
827
828 return true;
829 }
830
831 bool Design::BreakSinglePinNets()
832 {
833 const auto& pNoneNet = GetNoneNet();
834 if (pNoneNet == nullptr) return false;
835
836 auto& pinConnections = pNoneNet->GetPinConnections();
837 while (!pinConnections.empty())
838 {
839 const auto& pExistingPinConnection = pinConnections.back();
840
841 std::string newNetName = "$NC_" + pExistingPinConnection->GetComponent()->GetRefDes() + "_" + pExistingPinConnection->GetPin()->GetName();
842 auto pNewNet = std::make_shared<Net>(newNetName, -1);
843 pNewNet->AddPinConnection(pExistingPinConnection->GetComponent(), pExistingPinConnection->GetPin());
844 m_nets.push_back(pNewNet);
845 m_netsByName[pNewNet->GetName()] = pNewNet;
846
847 pinConnections.pop_back();
848 }
849
850 return true;
851 }
852
853
854 bool Design::CreateNetConnections(const std::shared_ptr<Odb::Lib::FileModel::Design::EdaDataFile::NetRecord>& pNetRecord, const std::shared_ptr<FileModel::Design::StepDirectory>& pStepDirectory)
855 {
856 for (const auto& pSubnetRecord : pNetRecord->m_subnetRecords)
857 {
859 {
860 const FileModel::Design::ComponentsFile* pComponentsFileToUse = nullptr;
861
862 auto side = pSubnetRecord->side;
863 if (side == BoardSide::Top)
864 {
865 pComponentsFileToUse = pStepDirectory->GetTopComponentsFile();
866 }
867 else //if (side == BoardSide::Bottom)
868 {
869 pComponentsFileToUse = pStepDirectory->GetBottomComponentsFile();
870 }
871
872 if (pComponentsFileToUse == nullptr) return false;
873
874 auto componentNumber = pSubnetRecord->componentNumber;
875 if (componentNumber >= pComponentsFileToUse->GetComponentRecords().size()) return false;
876
877 auto& pComponentRecord = pComponentsFileToUse->GetComponentRecords()[componentNumber];
878 if (pComponentRecord == nullptr) return false;
879
880 auto toeprintNumber = pSubnetRecord->toeprintNumber;
881 if (toeprintNumber >= pComponentRecord->m_toeprintRecords.size()) return false;
882
883 auto& pToeprintRecord = pComponentRecord->m_toeprintRecords[toeprintNumber];
884 if (pToeprintRecord == nullptr) return false;
885
886 auto& toeprintName = pToeprintRecord->name;
887 auto pinNumber = pToeprintRecord->pinNumber;
888 auto netNumber = pToeprintRecord->netNumber;
889 auto& refDes = pComponentRecord->compName;
890 //auto subnetNumber = pToeprintRecord->subnetNumber;
891
892 if (!CreatePinConnection(refDes, netNumber, pinNumber, toeprintName)) return false;
893 }
895 {
896 // ?
897 }
898 }
899
900 return true;
901 }
902
903} // namespace Odb::Lib::ProductModel
#define logwarn(s)
Definition Logger.h:20
const ComponentRecord::Vector & GetComponentRecords() const
std::map< std::string, std::shared_ptr< Component > > StringMap
Definition Component.h:43
std::vector< std::shared_ptr< Component > > Vector
Definition Component.h:42
std::uint64_t EstimateWireSizeBytes(bool includeNormalizedLists=false) const
Definition Design.cpp:470
const Component::Vector & GetComponents() const
Definition Design.cpp:64
std::shared_ptr< Net > GetNoneNet() const
Definition Design.cpp:105
const Part::StringMap & GetPartsByName() const
Definition Design.cpp:90
std::shared_ptr< Net > GetNet(const std::string &name) const
Definition Design.cpp:95
bool Build(std::string path)
Definition Design.cpp:110
const Net::Vector & GetNets() const
Definition Design.cpp:44
void FillConnectivityPackagePins(Protobuf::ProductModel::Connectivity &connectivityMsg) const
Definition Design.cpp:293
const Package::StringMap & GetPackagesByName() const
Definition Design.cpp:59
void AddConnectivityNetEntry(std::size_t netIndex, Protobuf::ProductModel::Connectivity &connectivityMsg) const
Definition Design.cpp:317
void FillConnectivityNetEntries(Protobuf::ProductModel::Connectivity &connectivityMsg) const
Definition Design.cpp:285
const Component::StringMap & GetComponentsByName() const
Definition Design.cpp:69
ConnectivityIndex BuildConnectivityIndex() const
Definition Design.cpp:236
void AddConnectivityPackagePinEntry(std::size_t packageIndex, Protobuf::ProductModel::Connectivity &connectivityMsg) const
Definition Design.cpp:329
const Package::Vector & GetPackages() const
Definition Design.cpp:54
void FillConnectivityPlacedComponents(const ConnectivityIndex &index, Protobuf::ProductModel::Connectivity &connectivityMsg) const
Definition Design.cpp:309
void AddConnectivityNetMemberEntry(const ConnectivityIndex &index, ConnectivityIndex::MembersByNetConstIt netMembersIt, Protobuf::ProductModel::Connectivity &connectivityMsg) const
Definition Design.cpp:345
const Part::Vector & GetParts() const
Definition Design.cpp:85
std::shared_ptr< FileModel::Design::FileArchive > GetFileModel() const
Definition Design.cpp:150
const Net::StringMap GetNetsByName() const
Definition Design.cpp:49
void FillConnectivityNetMembers(const ConnectivityIndex &index, Protobuf::ProductModel::Connectivity &connectivityMsg) const
Definition Design.cpp:301
std::shared_ptr< Component > GetComponent(const std::string &refDes) const
Definition Design.cpp:74
std::unique_ptr< Protobuf::ProductModel::Design > to_protobuf() const override
Definition Design.cpp:160
const std::string & GetName() const
Definition Design.cpp:34
const std::string & GetProductModel() const
Definition Design.cpp:39
void AddConnectivityPlacedComponentEntry(const ConnectivityIndex &index, std::size_t componentIndex, Protobuf::ProductModel::Connectivity &connectivityMsg) const
Definition Design.cpp:360
void from_protobuf(const Protobuf::ProductModel::Design &message) override
Definition Design.cpp:405
std::vector< std::shared_ptr< Net > > Vector
Definition Net.h:35
std::map< std::string, std::shared_ptr< Net > > StringMap
Definition Net.h:36
std::map< std::string, std::shared_ptr< Package > > StringMap
Definition Package.h:35
std::vector< std::shared_ptr< Package > > Vector
Definition Package.h:34
std::map< std::string, std::shared_ptr< Part > > StringMap
Definition Part.h:21
std::vector< std::shared_ptr< Part > > Vector
Definition Part.h:20
void FillProtobufMapWithSanitizedKeys(TPbMap &pbMap, const TEntries &entries)
void AssertAllStringFieldsAreValidUtf8(const google::protobuf::Message &msg, std::string_view msgName)
Debug-only assertion to verify all string fields in a message are valid UTF-8.
std::string ToUtf8(std::string_view input)
Converts input to valid UTF-8.
std::map< unsigned int, std::map< std::string, std::vector< unsigned int > > >::const_iterator MembersByNetConstIt
Definition Design.h:92
std::map< unsigned int, std::map< std::string, std::vector< unsigned int > > > membersByNet
Definition Design.h:88
std::map< std::string, std::map< unsigned int, Edge > > edgesByRefDes
Definition Design.h:87