- 积分
- 30299
- 明经币
- 个
- 注册时间
- 2016-9-16
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 fangmin723 于 2024-1-28 14:35 编辑
文字处理代码(关键代码,非全部):
- /// <summary>
- /// 文字添加或删除前后缀
- /// </summary>
- /// <param name="isadd">添加前后缀还是删除前后缀</param>
- /// <param name="ispre">是否添加前缀</param>
- /// <param name="prefix">前缀</param>
- /// <param name="issuf">是否添加后缀</param>
- /// <param name="suffix">后缀</param>
- public static void Tools_AddOrDelFix(bool isadd, bool ispre, string prefix, bool issuf, string suffix)
- {
- var objids = Tools_GetTextEntity();
- if (!objids.Any()) return;
- var Db = HostApplicationServices.WorkingDatabase;
- var Doc = Acap.DocumentManager.MdiActiveDocument;
- var Ed = Doc.Editor;
- using var dloc = Doc.LockDocument();
- using var tr = Db.TransactionManager.StartTransaction();
- objids.ForEach(id =>
- {
- var entity = tr.GetObject(id, OpenMode.ForWrite) as Entity;
- if (isadd)
- {
- if (entity is DBText text)
- text.TextString = $"{(ispre ? prefix : "")}{text.TextString}{(issuf ? suffix : "")}";
- if (entity is MText mText)
- mText.Contents = $"{(ispre ? prefix : "")}{mText.Contents}{(issuf ? suffix : "")}";
- }
- else
- {
- if (entity is DBText text)
- {
- if (ispre)
- text.TextString = text.TextString.TrimStart(prefix.ToCharArray());
- if (issuf)
- text.TextString = text.TextString.TrimEnd(suffix.ToCharArray());
- }
- if (entity is MText mText)
- {
- if (ispre)
- mText.Contents = mText.Contents.TrimStart(prefix.ToCharArray());
- if (issuf)
- mText.Contents = mText.Contents.TrimEnd(suffix.ToCharArray());
- }
- }
- });
- tr.Commit();
- }
- /// <summary>
- /// 文字内容替换
- /// </summary>
- /// <param name="regstr">正则表达式或者字符串</param>
- /// <param name="newstr">替换内容</param>
- /// <param name="iscase">是否区分大小写,默认 :false不区分</param>
- /// <param name="isall">是否全字匹配,默认:false不全字匹配</param>
- public static void Tools_ReplacePattern(string regstr, string newstr, bool iscase = false, bool isall = false)
- {
- var objids = Tools_GetTextEntity();
- if (!objids.Any()) return;
- if (isall) regstr = $"^{regstr}$";
- var Db = HostApplicationServices.WorkingDatabase;
- var Doc = Acap.DocumentManager.MdiActiveDocument;
- var Ed = Doc.Editor;
- using var dloc = Doc.LockDocument();
- using var tr = Db.TransactionManager.StartTransaction();
- objids.ForEach(id =>
- {
- var entity = tr.GetObject(id, OpenMode.ForWrite) as Entity;
- if (entity is DBText text)
- {
- text.TextString = iscase
- ? Regex.Replace(text.TextString, regstr, newstr)
- : Regex.Replace(text.TextString, regstr, newstr, RegexOptions.IgnoreCase);
- }
- if (entity is MText mText)
- mText.Contents = iscase
- ? Regex.Replace(mText.Contents, regstr, newstr)
- : Regex.Replace(mText.Contents, regstr, newstr, RegexOptions.IgnoreCase);
- });
- tr.Commit();
- }
2024.1.28:增加异常处理
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
|