Add portable PDF to DXF converter for BricsCAD workflows.
Includes vector/vectorize/raster modes, page review before convert, bundled OCR data, and a single-file AppData launcher build. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
commit
91625af67f
16 files changed
+3074
No files matched your search
@@ -0,0 +1,219 @@
|
||||
"""Detect BricsCAD and optionally convert DXF to DWG via scripted SAVEAS."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
import winreg
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
COMMON_BRICSCAD_PATHS = [
|
||||
r"C:\Program Files\Bricsys\BricsCAD V26 en_US\bricscad.exe",
|
||||
r"C:\Program Files\Bricsys\BricsCAD V25 en_US\bricscad.exe",
|
||||
r"C:\Program Files\Bricsys\BricsCAD V24 en_US\bricscad.exe",
|
||||
r"C:\Program Files\Bricsys\BricsCAD V23 en_US\bricscad.exe",
|
||||
r"C:\Program Files\Bricsys\BricsCAD V22 en_US\bricscad.exe",
|
||||
r"C:\Program Files\Bricsys\BricsCAD V21 en_US\bricscad.exe",
|
||||
r"C:\Program Files\Bricsys\BricsCAD V20 en_US\bricscad.exe",
|
||||
r"C:\Program Files (x86)\Bricsys\BricsCAD V26 en_US\bricscad.exe",
|
||||
r"C:\Program Files (x86)\Bricsys\BricsCAD V25 en_US\bricscad.exe",
|
||||
]
|
||||
|
||||
|
||||
def _registry_bricscad() -> Path | None:
|
||||
"""Try to find BricsCAD.exe via Windows uninstall / App Paths registry."""
|
||||
roots = [
|
||||
(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Bricsys"),
|
||||
(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\WOW6432Node\Bricsys"),
|
||||
(winreg.HKEY_CURRENT_USER, r"SOFTWARE\Bricsys"),
|
||||
]
|
||||
for root, subkey in roots:
|
||||
try:
|
||||
with winreg.OpenKey(root, subkey) as key:
|
||||
i = 0
|
||||
while True:
|
||||
try:
|
||||
name = winreg.EnumKey(key, i)
|
||||
except OSError:
|
||||
break
|
||||
i += 1
|
||||
try:
|
||||
with winreg.OpenKey(key, name) as ver_key:
|
||||
for value_name in ("InstallDir", "InstallPath", "Path"):
|
||||
try:
|
||||
val, _ = winreg.QueryValueEx(ver_key, value_name)
|
||||
exe = Path(val) / "bricscad.exe"
|
||||
if exe.is_file():
|
||||
return exe
|
||||
except OSError:
|
||||
continue
|
||||
# Sometimes the key itself points at the install folder
|
||||
try:
|
||||
default, _ = winreg.QueryValueEx(ver_key, None)
|
||||
if default:
|
||||
exe = Path(default) / "bricscad.exe"
|
||||
if exe.is_file():
|
||||
return exe
|
||||
except OSError:
|
||||
pass
|
||||
except OSError:
|
||||
continue
|
||||
except OSError:
|
||||
continue
|
||||
|
||||
# App Paths
|
||||
try:
|
||||
with winreg.OpenKey(
|
||||
winreg.HKEY_LOCAL_MACHINE,
|
||||
r"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\bricscad.exe",
|
||||
) as key:
|
||||
val, _ = winreg.QueryValueEx(key, None)
|
||||
exe = Path(val)
|
||||
if exe.is_file():
|
||||
return exe
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _scan_bricsys_folders() -> Path | None:
|
||||
bases = [
|
||||
Path(r"C:\Program Files\Bricsys"),
|
||||
Path(r"C:\Program Files (x86)\Bricsys"),
|
||||
]
|
||||
found: list[Path] = []
|
||||
for base in bases:
|
||||
if not base.is_dir():
|
||||
continue
|
||||
for child in base.iterdir():
|
||||
if not child.is_dir():
|
||||
continue
|
||||
exe = child / "bricscad.exe"
|
||||
if exe.is_file():
|
||||
found.append(exe)
|
||||
if not found:
|
||||
return None
|
||||
# Prefer highest version-looking folder name
|
||||
found.sort(key=lambda p: p.parent.name, reverse=True)
|
||||
return found[0]
|
||||
|
||||
|
||||
def find_bricscad() -> Path | None:
|
||||
"""Locate BricsCAD.exe on this machine, or return None."""
|
||||
env = os.environ.get("BRICSCAD_PATH")
|
||||
if env:
|
||||
p = Path(env)
|
||||
if p.is_file():
|
||||
return p
|
||||
|
||||
for candidate in COMMON_BRICSCAD_PATHS:
|
||||
p = Path(candidate)
|
||||
if p.is_file():
|
||||
return p
|
||||
|
||||
reg = _registry_bricscad()
|
||||
if reg:
|
||||
return reg
|
||||
|
||||
return _scan_bricsys_folders()
|
||||
|
||||
|
||||
def is_bricscad_available() -> bool:
|
||||
return find_bricscad() is not None
|
||||
|
||||
|
||||
def dxf_to_dwg(
|
||||
dxf_path: str | Path,
|
||||
dwg_path: str | Path | None = None,
|
||||
bricscad_exe: str | Path | None = None,
|
||||
timeout_sec: float = 120.0,
|
||||
) -> Path:
|
||||
"""
|
||||
Open a DXF in BricsCAD and SAVEAS to DWG, then quit.
|
||||
|
||||
Uses a temporary AutoLISP/script file passed with /b (batch script).
|
||||
"""
|
||||
dxf_path = Path(dxf_path).resolve()
|
||||
if not dxf_path.is_file():
|
||||
raise FileNotFoundError(f"DXF not found: {dxf_path}")
|
||||
|
||||
if dwg_path is None:
|
||||
dwg_path = dxf_path.with_suffix(".dwg")
|
||||
else:
|
||||
dwg_path = Path(dwg_path).resolve()
|
||||
|
||||
exe = Path(bricscad_exe) if bricscad_exe else find_bricscad()
|
||||
if exe is None or not exe.is_file():
|
||||
raise FileNotFoundError(
|
||||
"BricsCAD was not found. Install BricsCAD or set BRICSCAD_PATH."
|
||||
)
|
||||
|
||||
# BricsCAD script: open DXF, save as DWG, quit
|
||||
# Use forward slashes in paths for script compatibility
|
||||
dxf_s = str(dxf_path).replace("\\", "/")
|
||||
dwg_s = str(dwg_path).replace("\\", "/")
|
||||
|
||||
script_lines = [
|
||||
f'_.OPEN "{dxf_s}"',
|
||||
f'_.SAVEAS 2013 "{dwg_s}"',
|
||||
"_.QUIT",
|
||||
"",
|
||||
]
|
||||
|
||||
with tempfile.NamedTemporaryFile(
|
||||
mode="w",
|
||||
suffix=".scr",
|
||||
delete=False,
|
||||
encoding="utf-8",
|
||||
newline="\n",
|
||||
) as tmp:
|
||||
tmp.write("\n".join(script_lines))
|
||||
script_path = Path(tmp.name)
|
||||
|
||||
try:
|
||||
# /b runs a script; /nologo reduces splash noise when supported
|
||||
cmd = [str(exe), "/b", str(script_path)]
|
||||
proc = subprocess.Popen(
|
||||
cmd,
|
||||
cwd=str(dxf_path.parent),
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
deadline = time.time() + timeout_sec
|
||||
while time.time() < deadline:
|
||||
if proc.poll() is not None:
|
||||
break
|
||||
if dwg_path.is_file() and dwg_path.stat().st_size > 0:
|
||||
# Give BricsCAD a moment to finish writing, then try to close
|
||||
time.sleep(1.5)
|
||||
if proc.poll() is None:
|
||||
try:
|
||||
proc.terminate()
|
||||
except Exception:
|
||||
pass
|
||||
break
|
||||
time.sleep(0.5)
|
||||
else:
|
||||
try:
|
||||
proc.kill()
|
||||
except Exception:
|
||||
pass
|
||||
raise TimeoutError(
|
||||
f"BricsCAD did not finish converting within {timeout_sec:.0f}s."
|
||||
)
|
||||
|
||||
if not dwg_path.is_file():
|
||||
raise RuntimeError(
|
||||
"BricsCAD finished but the DWG file was not created. "
|
||||
"Try opening the DXF manually and using SAVEAS."
|
||||
)
|
||||
return dwg_path
|
||||
finally:
|
||||
try:
|
||||
script_path.unlink(missing_ok=True)
|
||||
except OSError:
|
||||
pass
|
||||
Reference in new issue
Block a user