Add portable Mentor schematic batch exporter
This commit is contained in:
commit
40fbb1893a
26 files changed
+3622
No files matched your search
@@ -0,0 +1,6 @@
|
||||
@echo off
|
||||
setlocal
|
||||
call "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars32.bat" >nul
|
||||
if errorlevel 1 exit /b %errorlevel%
|
||||
cl /std:c++17 /nologo /W4 /EHsc /O2 /MT "%~dp0native_read.cpp" /Fe"%~dp0native_read.exe" /Fo"%TEMP%\mentorexport-native.obj"
|
||||
exit /b %errorlevel%
|
||||
@@ -0,0 +1,91 @@
|
||||
Option Explicit
|
||||
Dim db, initResult, sheet, obj, cons, fso, output, total, seen, column, definition
|
||||
Set fso = CreateObject("Scripting.FileSystemObject")
|
||||
Set output = fso.CreateTextFile(WScript.Arguments(1), True, True)
|
||||
Set db = CreateObject("CMS.CeObjectDefsICDB")
|
||||
initResult = db.InitFromProjectFile(WScript.Arguments(2), "r", WScript.Arguments(0), WScript.Arguments(3), WScript.Arguments(4), "DxDesigner")
|
||||
output.WriteLine "{""kind"":""initialization"",""result"":" & V(initResult) & ",""read_only"":" & V(db.ReadOnly) & ",""project"":" & Q(db.PrjFile) & "}"
|
||||
If Not initResult Or LCase(db.PrjFile) <> LCase(WScript.Arguments(0)) Then
|
||||
output.Close
|
||||
WScript.Quit 3
|
||||
End If
|
||||
total = 0
|
||||
Set seen = CreateObject("Scripting.Dictionary")
|
||||
db.LoadAllDeferredPages
|
||||
For Each sheet In db.Sheets
|
||||
output.WriteLine "{""kind"":""sheet"",""name"":" & Q(sheet.Name) & "}"
|
||||
For Each column In sheet.Columns
|
||||
Set definition = column.ConstraintDef
|
||||
output.WriteLine "{""kind"":""definition"",""sheet"":" & Q(sheet.Name) & ",""name"":" & Q(definition.Name) & ",""title"":" & Q(definition.Title) & ",""description"":" & Q(definition.Description) & ",""data_type"":" & V(definition.DataType) & ",""unit_type"":" & V(definition.UnitType) & ",""unit"":" & V(definition.Unit) & ",""display_unit"":" & V(definition.DisplayUnit) & ",""default"":" & V(definition.Default) & "}"
|
||||
Next
|
||||
For Each obj In sheet.Objects
|
||||
DumpObject sheet.Name, obj
|
||||
Next
|
||||
Next
|
||||
output.WriteLine "{""kind"":""complete"",""count"":" & CStr(total) & ",""objects"":" & CStr(seen.Count) & "}"
|
||||
output.Close
|
||||
Set db = Nothing
|
||||
WScript.Echo "Constraints read: " & CStr(total)
|
||||
|
||||
Sub DumpObject(sheetName, current)
|
||||
Dim key, constraint, child, children, actual, defaultValue, overridden, resolvedDefault, errorText, unitValue, def
|
||||
key = sheetName & vbTab & current.Level & vbTab & CStr(current.AthenaId) & vbTab & current.Name
|
||||
If seen.Exists(key) Then Exit Sub
|
||||
seen.Add key, True
|
||||
If seen.Count > 100000 Then WScript.Quit 4
|
||||
For Each constraint In current.Constraints
|
||||
actual = "" : defaultValue = "" : overridden = "" : resolvedDefault = "" : errorText = ""
|
||||
unitValue = ""
|
||||
On Error Resume Next
|
||||
actual = constraint.Value
|
||||
If Err.Number <> 0 Then errorText = errorText & "Value: " & Err.Description & "; " : Err.Clear
|
||||
defaultValue = constraint.DefaultValue
|
||||
If Err.Number <> 0 Then errorText = errorText & "DefaultValue: " & Err.Description & "; " : Err.Clear
|
||||
overridden = constraint.IsOverridden
|
||||
If Err.Number <> 0 Then errorText = errorText & "IsOverridden: " & Err.Description & "; " : Err.Clear
|
||||
resolvedDefault = current.IsResolvedDefaultValue(constraint.Name, CStr(actual))
|
||||
If Err.Number <> 0 Then errorText = errorText & "IsResolvedDefaultValue: " & Err.Description & "; " : Err.Clear
|
||||
Set def = constraint.ConstraintDef
|
||||
unitValue = constraint.GetStringValue(def.Unit)
|
||||
If Err.Number <> 0 Then errorText = errorText & "GetStringValue: " & Err.Description & "; " : Err.Clear
|
||||
On Error GoTo 0
|
||||
output.WriteLine "{""kind"":""constraint"",""sheet"":" & Q(sheetName) & ",""object"":" & Q(current.Name) & ",""level"":" & Q(current.Level) & ",""athena_id"":" & Q(current.AthenaId) & ",""name"":" & Q(constraint.Name) & ",""value"":" & V(actual) & ",""default_value"":" & V(defaultValue) & ",""is_overridden"":" & V(overridden) & ",""equals_resolved_default"":" & V(resolvedDefault) & ",""value_in_definition_unit"":" & Q(unitValue) & ",""unit"":" & V(def.Unit) & ",""unit_type"":" & V(def.UnitType) & ",""error"":" & Q(errorText) & "}"
|
||||
total = total + 1
|
||||
Next
|
||||
Set children = current.GetChildObjects
|
||||
If Not children Is Nothing Then
|
||||
For Each child In children
|
||||
DumpObject sheetName, child
|
||||
Next
|
||||
End If
|
||||
Set child = Nothing
|
||||
Set children = Nothing
|
||||
End Sub
|
||||
|
||||
Function Q(value)
|
||||
Dim s
|
||||
If IsNull(value) Or IsEmpty(value) Then
|
||||
s = ""
|
||||
Else
|
||||
s = CStr(value)
|
||||
End If
|
||||
s = Replace(s, "\", "\\")
|
||||
s = Replace(s, Chr(34), "\" & Chr(34))
|
||||
s = Replace(s, vbCr, "\r")
|
||||
s = Replace(s, vbLf, "\n")
|
||||
s = Replace(s, vbTab, "\t")
|
||||
Q = Chr(34) & s & Chr(34)
|
||||
End Function
|
||||
|
||||
Function V(value)
|
||||
Select Case VarType(value)
|
||||
Case 0, 1
|
||||
V = "null"
|
||||
Case 11
|
||||
V = LCase(CStr(value))
|
||||
Case 2, 3, 4, 5, 6, 14, 17
|
||||
V = Replace(CStr(value), ",", ".")
|
||||
Case Else
|
||||
V = Q(value)
|
||||
End Select
|
||||
End Function
|
||||
@@ -0,0 +1,216 @@
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <windows.h>
|
||||
#include <bcrypt.h>
|
||||
#pragma comment(lib, "bcrypt.lib")
|
||||
// Disposable research client for the locally verified EE7.9.5 x86 PartsDB40 ABI.
|
||||
// Query a copied PDB. No CAD process or private replacement address is invoked.
|
||||
static_assert(sizeof(void *) == 4, "This research ABI requires an x86 build.");
|
||||
struct ObjectGuard {
|
||||
void *value;
|
||||
ULONG(__stdcall *release)(void *);
|
||||
~ObjectGuard() {
|
||||
if (value)
|
||||
release(value);
|
||||
}
|
||||
};
|
||||
struct ModuleGuard {
|
||||
HMODULE value;
|
||||
~ModuleGuard() {
|
||||
if (value)
|
||||
FreeLibrary(value);
|
||||
}
|
||||
};
|
||||
static void str(const char *p) {
|
||||
std::string utf8;
|
||||
if (p && *p) {
|
||||
int count =
|
||||
MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, p, -1, nullptr, 0);
|
||||
if (!count)
|
||||
throw std::runtime_error(
|
||||
"Metadata encoding is invalid for the native ANSI code page");
|
||||
std::vector<wchar_t> wide(count);
|
||||
if (!MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, p, -1, wide.data(),
|
||||
count))
|
||||
throw std::runtime_error("ANSI decoding failed");
|
||||
int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wide.data(),
|
||||
count - 1, nullptr, 0, nullptr, nullptr);
|
||||
if (!size)
|
||||
throw std::runtime_error("UTF-8 conversion failed");
|
||||
utf8.resize(size);
|
||||
if (!WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wide.data(),
|
||||
count - 1, utf8.data(), size, nullptr, nullptr))
|
||||
throw std::runtime_error("UTF-8 conversion failed");
|
||||
}
|
||||
putchar('"');
|
||||
for (unsigned char c : utf8) {
|
||||
if (c == '"' || c == '\\')
|
||||
printf("\\%c", c);
|
||||
else if (c < 32)
|
||||
printf("\\u%04x", c);
|
||||
else
|
||||
putchar(c);
|
||||
}
|
||||
putchar('"');
|
||||
}
|
||||
template <class T> T proc(HMODULE h, const char *name) {
|
||||
auto p = GetProcAddress(h, name);
|
||||
if (!p)
|
||||
throw std::runtime_error(name);
|
||||
return reinterpret_cast<T>(p);
|
||||
}
|
||||
using ReadString = HRESULT(__stdcall *)(void *, int, const char **);
|
||||
using ReadInt = HRESULT(__stdcall *)(void *, int, int *);
|
||||
using ReadList = HRESULT(__stdcall *)(void *, int, const int **, int *);
|
||||
using ReadPair = HRESULT(__stdcall *)(void *, int, int *, int *);
|
||||
using ReadPoint = HRESULT(__stdcall *)(void *, int, const int **);
|
||||
|
||||
static void check(HRESULT hr) {
|
||||
if (FAILED(hr)) throw std::runtime_error("Native getter failed");
|
||||
}
|
||||
static void string_field(HMODULE module, void *db, int id, const char *symbol,
|
||||
const char *field) {
|
||||
const char *value = nullptr;
|
||||
HRESULT hr = proc<ReadString>(module, symbol)(db, id, &value);
|
||||
putchar(','); str(field); printf(":{\"hr\":%ld,\"value\":", hr);
|
||||
if (SUCCEEDED(hr)) str(value); else printf("null");
|
||||
putchar('}');
|
||||
}
|
||||
static std::vector<int> ids(HMODULE module, void *db, int id, const char *symbol) {
|
||||
const int *data = nullptr; int count = 0;
|
||||
check(proc<ReadList>(module, symbol)(db, id, &data, &count));
|
||||
if (count < 0 || count > 100000 || (count && !data))
|
||||
throw std::runtime_error("Invalid native list");
|
||||
return count ? std::vector<int>(data, data + count) : std::vector<int>();
|
||||
}
|
||||
static bool verified_dll(const std::string &path, const char *expected) {
|
||||
FILE *file = nullptr;
|
||||
if (fopen_s(&file, path.c_str(), "rb") || !file) return false;
|
||||
BCRYPT_ALG_HANDLE algorithm = nullptr;
|
||||
BCRYPT_HASH_HANDLE hash = nullptr;
|
||||
unsigned char result[32], block[65536];
|
||||
bool ok = BCryptOpenAlgorithmProvider(&algorithm, BCRYPT_SHA256_ALGORITHM, nullptr, 0) == 0;
|
||||
if (ok) ok = BCryptCreateHash(algorithm, &hash, nullptr, 0, nullptr, 0, 0) == 0;
|
||||
size_t count;
|
||||
while (ok && (count = fread(block, 1, sizeof(block), file)) != 0)
|
||||
ok = BCryptHashData(hash, block, static_cast<ULONG>(count), 0) == 0;
|
||||
if (ferror(file)) ok = false;
|
||||
if (ok) ok = BCryptFinishHash(hash, result, sizeof(result), 0) == 0;
|
||||
fclose(file);
|
||||
if (hash) BCryptDestroyHash(hash);
|
||||
if (algorithm) BCryptCloseAlgorithmProvider(algorithm, 0);
|
||||
if (!ok) return false;
|
||||
char hex[65];
|
||||
for (unsigned i = 0; i < 32; ++i) sprintf_s(hex + i * 2, 65 - i * 2, "%02x", result[i]);
|
||||
return strcmp(hex, expected) == 0;
|
||||
}
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 4) return 64;
|
||||
bool cell = !strcmp(argv[1], "cell");
|
||||
if (!cell && strcmp(argv[1], "parts")) return 64;
|
||||
SetDllDirectoryA(argv[2]);
|
||||
std::string filename = std::string(argv[2]) + (cell ? "\\CellDB40.dll" : "\\PartsDB40.dll");
|
||||
if (!verified_dll(filename, cell ? "e3fb93a0b5e5d2d027a82f71d1ee13d497598f709ced54c9145a781af6a54d26"
|
||||
: "f24d44eaa13f28550317711df6a6241e227ae56805baa73c8f3020bde513ad0d")) {
|
||||
fprintf(stderr, "Unverified DLL ABI: SHA256 does not match the validated EE7.9.5 library.\n");
|
||||
return 65;
|
||||
}
|
||||
HMODULE h = LoadLibraryA(filename.c_str()); ModuleGuard module{h};
|
||||
if (!h) return 2;
|
||||
try {
|
||||
using Factory = HRESULT(__cdecl *)(void **);
|
||||
using Loader = HRESULT(__stdcall *)(void *, const char *, int);
|
||||
using GetEnum = HRESULT(__stdcall *)(void *, void **);
|
||||
using Next = HRESULT(__stdcall *)(void *, ULONG, int *, ULONG *);
|
||||
using Release = ULONG(__stdcall *)(void *);
|
||||
const char *klass = cell ? "CellDB" : "PartsDB";
|
||||
auto decorated = [klass](const char *method, const char *suffix) {
|
||||
return std::string("?") + method + "@" + klass + "@@" + suffix;
|
||||
};
|
||||
auto symbol = [&](const char *method, const char *suffix) {
|
||||
return GetProcAddress(h, decorated(method, suffix).c_str());
|
||||
};
|
||||
auto release = reinterpret_cast<Release>(symbol("Release", "UAGKXZ"));
|
||||
auto load = reinterpret_cast<Loader>(symbol("Loader", "UAGJPBDH@Z"));
|
||||
if (!release || !load) throw std::runtime_error("Missing required exports");
|
||||
void *db = nullptr;
|
||||
check(proc<Factory>(h, cell ? "GetCellDBPtr" : "GetPartsDBPtr")(&db));
|
||||
ObjectGuard db_guard{db, release}; if (!db) return 3;
|
||||
check(load(db, argv[3], 0));
|
||||
auto enumerate = reinterpret_cast<GetEnum>(symbol(cell ? "GetCellEnum" : "GetPartEnum", "UAGJPAPAUIEnumInt@@@Z"));
|
||||
auto next = proc<Next>(h, cell ? "?Next@EnumCellDBCell@@UAGJKPAHPAK@Z" : "?Next@EnumPartsDBPart@@UAGJKPAHPAK@Z");
|
||||
auto release_enum = proc<Release>(h, cell ? "?Release@EnumCellDBCell@@UAGKXZ" : "?Release@EnumPartsDBPart@@UAGKXZ");
|
||||
if (!enumerate) throw std::runtime_error("Missing enumerator export");
|
||||
void *e = nullptr; check(enumerate(db, &e)); ObjectGuard enum_guard{e, release_enum};
|
||||
auto name = reinterpret_cast<ReadString>(symbol(cell ? "GetName" : "GetPartNumber", "UAGJHPAPBD@Z"));
|
||||
if (!e || !name) return 4;
|
||||
printf("{\"mode\":\"%s\"", argv[1]);
|
||||
if (cell) {
|
||||
using BaseUnit = HRESULT(__stdcall *)(void *, int *);
|
||||
int unit = -1;
|
||||
HRESULT unit_hr = proc<BaseUnit>(h, "?GetCellDBBaseUnit@CellDB@@UAGJPAH@Z")(db, &unit);
|
||||
printf(",\"base_unit_hr\":%ld,\"base_unit_code\":%d", unit_hr, unit);
|
||||
}
|
||||
printf(",\"records\":[");
|
||||
unsigned count = 0;
|
||||
for (;;) {
|
||||
int id = -1; ULONG fetched = 0; HRESULT hr = next(e, 1, &id, &fetched);
|
||||
check(hr); if (!fetched) break;
|
||||
if (++count > 100000) return 6;
|
||||
const char *n = nullptr; check(name(db, id, &n));
|
||||
if (count > 1) putchar(','); printf("{\"id\":%d,\"name\":", id); str(n);
|
||||
if (cell) {
|
||||
string_field(h, db, id, "?GetDescription@CellDB@@UAGJHPAPBD@Z", "description");
|
||||
auto pins = ids(h, db, id, "?GetPins@CellDB@@UAGJHPAPBHPAH@Z");
|
||||
printf(",\"pins\":["); bool first = true;
|
||||
for (int pin : pins) {
|
||||
if (!first) putchar(','); first = false; printf("{\"id\":%d", pin);
|
||||
string_field(h, db, pin, "?GetPinName@CellDB@@UAGJHPAPBD@Z", "name");
|
||||
string_field(h, db, pin, "?GetPinPadstackName@CellDB@@UAGJHPAPBD@Z", "padstack");
|
||||
const int *point = nullptr; int rotation = 0;
|
||||
HRESULT hp = proc<ReadPoint>(h, "?GetPinLocation@CellDB@@UAGJHPAPBH@Z")(db, pin, &point);
|
||||
HRESULT rotation_hr = proc<ReadInt>(h, "?GetPinRotation@CellDB@@UAGJHPAH@Z")(db, pin, &rotation);
|
||||
printf(",\"location_hr\":%ld,\"rotation_hr\":%ld,\"rotation_raw\":%d,\"xy_raw\":", hp, rotation_hr, rotation);
|
||||
if (SUCCEEDED(hp) && point) printf("[%d,%d]", point[0], point[1]); else printf("null");
|
||||
putchar('}');
|
||||
}
|
||||
putchar(']');
|
||||
} else {
|
||||
string_field(h, db, id, "?GetPartName@PartsDB@@UAGJHPAPBD@Z", "part_name");
|
||||
string_field(h, db, id, "?GetPartLabel@PartsDB@@UAGJHPAPBD@Z", "part_label");
|
||||
string_field(h, db, id, "?GetTopCellName@PartsDB@@UAGJHPAPBD@Z", "top_cell_name");
|
||||
string_field(h, db, id, "?GetBottomCellName@PartsDB@@UAGJHPAPBD@Z", "bottom_cell_name");
|
||||
printf(",\"properties\":["); bool first = true;
|
||||
for (int property : ids(h, db, id, "?GetPartProperties@PartsDB@@UAGJHPAPBHPAH@Z")) {
|
||||
int registry = -1, value = -1;
|
||||
check(proc<ReadPair>(h, "?GetProperty@PartsDB@@UAGJHPAH0@Z")(db, property, ®istry, &value));
|
||||
if (!first) putchar(','); first = false;
|
||||
printf("{\"property_id\":%d,\"registry_id\":%d,\"value_id\":%d", property, registry, value);
|
||||
string_field(h, db, registry, "?GetPrpName@PartsDB@@UAGJHPAPBD@Z", "name");
|
||||
string_field(h, db, value, "?GetValueText@PartsDB@@UAGJHPAPBD@Z", "text"); putchar('}');
|
||||
}
|
||||
printf("],\"symbols\":["); first = true;
|
||||
for (int sym : ids(h, db, id, "?GetSymDfnList@PartsDB@@UAGJHPAPBHPAH@Z")) {
|
||||
if (!first) putchar(','); first = false; printf("{\"id\":%d", sym);
|
||||
string_field(h, db, sym, "?GetSymDfnName@PartsDB@@UAGJHPAPBD@Z", "name");
|
||||
printf(",\"pins\":["); bool first_pin = true;
|
||||
for (int pin : ids(h, db, sym, "?GetSymPinList@PartsDB@@UAGJHPAPBHPAH@Z")) {
|
||||
if (!first_pin) putchar(','); first_pin = false; printf("{\"id\":%d", pin);
|
||||
string_field(h, db, pin, "?GetSymPinName@PartsDB@@UAGJHPAPBD@Z", "name");
|
||||
int ordinal = -1; HRESULT ordinal_hr = proc<ReadInt>(h, "?GetSymPinOrdinal@PartsDB@@UAGJHPAH@Z")(db, pin, &ordinal);
|
||||
printf(",\"ordinal_hr\":%ld,\"ordinal\":%d}", ordinal_hr, ordinal);
|
||||
}
|
||||
printf("]}");
|
||||
}
|
||||
printf("]");
|
||||
}
|
||||
putchar('}');
|
||||
}
|
||||
printf("],\"count\":%u}\n", count); return 0;
|
||||
} catch (const std::exception &e) {
|
||||
fprintf(stderr, "read failed: %s\n", e.what()); return 9;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Reference in new issue
Block a user