Created
August 10, 2020 22:53
-
-
Save ggajoch/7f35a65b938a345ca0d0ebf8e6ffc96e to your computer and use it in GitHub Desktop.
altium file modifier - remove text from pcblib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, struct, copy | |
import re | |
import pythoncom | |
from win32com.storagecon import * | |
import glob | |
import progressbar | |
from PcbLib.PcbLib import PcbLib | |
mode = STGM_READWRITE|STGM_SHARE_EXCLUSIVE|STGM_DIRECT | |
# filename = 'xd.PcbLib' | |
# name = 'ACM90V' | |
# if 1: | |
for filename in progressbar.progressbar(list(glob.iglob(r'S:\WSN\altium_lib_db\PCB\**\*.PcbLib', recursive=True))): | |
name = os.path.basename(filename)[:-7] | |
# pcb = PcbLib(filename) | |
print(filename, name) | |
if len(name) >= 32: | |
name = name[:31] | |
istorage = pythoncom.StgOpenStorageEx(filename, mode, STGFMT_STORAGE, 0, pythoncom.IID_IStorage) | |
istorage2 = istorage.OpenStorage(name, None, mode, None, 0) | |
istream = istorage2.OpenStream("Data", None, mode, 0) | |
data = istream.read(-1) | |
def replace(s, data): | |
m = b'''(\x05[\xf0-\xff]\x00\x00\x00.{100,255}?\x00\x00\x00\x01(d|-))''' | |
x = re.findall(m, data, re.DOTALL) | |
# print(x) | |
# assert len(x) == 2 | |
# print(data) | |
for i in x: | |
(l,) = struct.unpack('<I', i[0][1:5]) | |
# print(l, len(i[0])) | |
# print(i[0]) | |
assert len(i[0]) == l + 1 + 4 + 4 + 1 + 1 | |
(data, nr) = re.subn(m, b'', data, flags=re.DOTALL) | |
return (data, nr) | |
(data, acc) = replace(b'(d|-)', data) | |
istream.Seek(0, 0) | |
istream.Write(data) | |
istream.SetSize(len(data)) | |
istream.Commit(STGC_OVERWRITE) | |
istorage2.Commit(STGC_OVERWRITE) | |
istorage.Commit(STGC_OVERWRITE) | |
istream = istorage2.OpenStream("Header", None, mode, 0) | |
data = istream.read(-1) | |
assert len(data) == 4 | |
(val,) = struct.unpack('<I', data) | |
val -= acc | |
istream.Seek(0, 0) | |
istream.Write(struct.pack('<I', val)) | |
istream.Commit(STGC_OVERWRITE) | |
istorage2.Commit(STGC_OVERWRITE) | |
istorage.Commit(STGC_OVERWRITE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment