AUTOCAD-二次开发-C#-自学-实例06-字体替代 代码

软件: AUTOCAD
全方位数据报表
许可分析

许可分析

免费体验
识别闲置、及时回收
许可优化

许可优化

免费体验
多维度智能分析
许可分析

许可分析

免费体验
减少成本、盘活许可
许可优化

许可优化

免费体验

A Deep Dive into Font Replacement within Autocad and C: An Example Walkthrough

In the realm of Autocad programming, one oftentimes encounters scenarios requiring custom modifications to the CAD environment, notably the replacement of specific fonts within text styles. This article aims to explore a practical `.NET` solution that utilizes `C` within the Autocad automation domain, focusing on the process of replacing a missing custom font with a default set of CAD fonts.

Requirement Scenarion:

The task involves automating the replacement of missing custom fonts (in `.shx` and `.swf` formats) with default fonts (`GBHZFS` and `SIMPLEX`, respectively) for `Autocad`'s text styles. This process is executed through Autocad's host application services, ensuring the alteration is applied consistently across all text styles potentially affected by the absence of the specified fonts within the current work environment.

StepbyStep Code Explanation:

The solution outlined within this article takes advantage of `Autocad.DatabaseServices`, `Autocad.Runtime`, and `IFoxCAD.Cad` namespaces to access components of the Autocad application. The program is presented as a `CommandMethod` named `DYH_ZTTD` (Chinese for "Font Replacement" or "字替换"). It introduces objects such as `DBTrans` for transactions, `HostApplicationServices` for accessing host application functions, and specific text style records management.

Upon execution, the following script implements a structured approach to locate and handle missing fonts:

1. Initialization of Transaction: A `DBTrans` object is first created to manage the current transaction context.

2. Accessing Host Applications: The `HostApplicationServices.Current` method retrieves the current host application, which becomes the interface for executing various Autocad commands and functions.

欢迎浏览: AUTOCAD-二次开发-C#-自学-实例06-字体替代 代码


3. Obtaining Text Style Table: It then seeks and retrieves the collection of all text style records from the current graphic through the `TextStyleTable.GetRecords()` method.

4. Iterative Asset Review: Each text style record is evaluated in a `foreach` loop.

Font Identification & Replacement Process: For each text style that lacks a defined `.shx` or `.swf` font (indicating that the custom font itself is either missing or incorrectly referenced), the program enters a destructible `trycatch` block.

File Path Search: Utilizing `host.FindFile()`, the program attempts to locate the specified font file within the `Env.Database` directory, specifically through `FindFileHint.CompiledShapeFile`.

Replacement Protocol: If the file is absent, the program captures this exception and utilizes the `Env.Editor.WriteMessage()` function to log a notification to the command window alongside the current style name and the intended font file path. This serves an informative dualpurpose of alerting the user while ensuring no runtime errors subsequently proceed.

Font Resolution: Once the error is handled, the custom font file path is replaced with `"GBHZFS"` for `.shx` files or `"SIMPLEX"` for `.swf` files. This step is not strictly required as these default names point to predefined font locations within `Autocad`, but it adds to the runtime visibility and user experience.

5. Optional Regeneration for Updates: Finally, the command suggests invoking `Env.Editor.Regen()` to update and regenerate the current graphic after replacing all problematic text styles, ensuring the spatial and formatting changes are visible across the entire document.

Conclusion:

This `C` script for `Autocad` provides a clear, modular, and automated solution for font replacement within text styles, tackling the challenge of maintaining a consistent and errorfree text display in complex or extensive CAD environments. The adaptive approach thanks to exception handling ensures system reliability and userfriendly error notification, pertinent in a professional engineering or architectural workflow.


武汉格发信息技术有限公司,格发许可优化管理系统可以帮你评估贵公司软件许可的真实需求,再低成本合规性管理软件许可,帮助贵司提高软件投资回报率,为软件采购、使用提供科学决策依据。支持的软件有: CAD,CAE,PDM,PLM,Catia,Ugnx, AutoCAD, Pro/E, Solidworks ,Hyperworks, Protel,CAXA,OpenWorks LandMark,MATLAB,Enovia,Winchill,TeamCenter,MathCAD,Ansys, Abaqus,ls-dyna, Fluent, MSC,Bentley,License,UG,ug,catia,Dassault Systèmes,AutoDesk,Altair,autocad,PTC,SolidWorks,Ansys,Siemens PLM Software,Paradigm,Mathworks,Borland,AVEVA,ESRI,hP,Solibri,Progman,Leica,Cadence,IBM,SIMULIA,Citrix,Sybase,Schlumberger,MSC Products...

index-foot-banner-pc index-foot-banner-phone

点击一下 免费体验万千客户信任的许可优化平台

与100+大型企业一起,将本增效

与100+大型企业一起,将本增效

申请免费体验 申请免费体验