使用mtext将一些特定文本从文件名打印到dwg

时间:2016-08-05 10:33:20

标签: c# autocad

我有一个dwg文件,其名称为 534-W1A-R1.dwg 我想在我的dwg文件中使用MText仅打印 W1A-R1

2 个答案:

答案 0 :(得分:0)

System.IO.Path.GetFileNameWithoutExtension("534-W1A-R1.dwg").Substring(fileName.IndexOf('-') + 1)

返回“W1A-R1”

或者,使用正则表达式:

Regex.Match("534-W1A-R1.dwg", @"-(.+)\.").Groups[1].Value

答案 1 :(得分:0)

您可以使用Databse类来提取它的文件名。然后像往常一样 - 创建新的MText并添加到ModelSpace BlockTableRecord

[CommandMethod("AddFileNameText")]
public static void AddFileNameText()
{
    Document acDoc = Application.DocumentManager.MdiActiveDocument;

    var db = acDoc.Database;
    using (Transaction transaction = db.TransactionManager.StartTransaction())
    {
        ObjectId idModelSpace = SymbolUtilityServices.GetBlockModelSpaceId(db);

        BlockTableRecord modelSpace =
            transaction.GetObject(idModelSpace, OpenMode.ForWrite) as
            BlockTableRecord;

        MText acMText = new MText();
        acMText.SetDatabaseDefaults();

        string strFilePath = db.Filename; //c:\534-W1A-R1.dwg
        strFilePath = 
            System.IO.Path.GetFileNameWithoutExtension(strFilePath); //534-W1A-R1

        //Do ANY text processing            
        if (strFilePath.IndexOf('-') > 0)
            strFilePath = strFilePath.Substring(
                strFilePath.IndexOf('-') + 1); //W1A-R1

        acMText.Contents = strFilePath;

        modelSpace.AppendEntity(acMText);
        transaction.AddNewlyCreatedDBObject(acMText, true);

        transaction.Commit();
    }
}