明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 711|回复: 0

Comparing Explode with ExplodeFragments on AutoCAD MText

[复制链接]
发表于 2011-2-24 11:25:35 | 显示全部楼层 |阅读模式
This request was posted by Mike C as a comment on the last post:
Could you expand this article to handle MTEXT? It would be nice to contrast the Explode and ExplodeFragments methods.
As I hadn’t actually used MText.ExplodeFragments() before (at least not that I could remember), I thought it was indeed worth spending some time on today.
Before launching into the code, a few words on using Entity.Explode() or MText.ExplodeFragments(), specifically with respect to MText. Entity.Explode() works very well on MText objects: if you want to generate physical AutoCAD geometry from MText (in this case creating DBText objects or possibly a number of smaller MText objects, in the case of multi-column MText), then this is the way to go. MText.ExplodeFragments() is a good way to get at the data inside your MText – you get a function call reporting each line (or fragment) of your MText – and this is presumably quite handy for performing tasks such as stripping control codes, etc.
The below C# code extends the code in the last post to more explicitly deal with MText. It dumps the MText fragments found to the command-line, and optionally allows you to explode the MText entities anyway.
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

namespace Explosion
{

public
class
Commands
  {
    [CommandMethod("EXP", CommandFlags.UsePickSet)]

public
void ExplodeEntities()
    {

Document doc =

Application.DocumentManager.MdiActiveDocument;

Database db = doc.Database;

Editor ed = doc.Editor;


// Ask user to select entities


PromptSelectionOptions pso =

new
PromptSelectionOptions();
      pso.MessageForAdding = "\nSelect objects to explode: ";
      pso.AllowDuplicates = false;
      pso.AllowSubSelections = true;
      pso.RejectObjectsFromNonCurrentSpace = true;
      pso.RejectObjectsOnLockedLayers = false;


PromptSelectionResult psr = ed.GetSelection(pso);

if (psr.Status != PromptStatus.OK)

return;


// Check whether to erase the original(s)


bool eraseOrig = false,
          explodeMText = false;


if (psr.Value.Count > 0)
      {

PromptKeywordOptions pko =

new
PromptKeywordOptions("\nErase original objects?");
        pko.AllowNone = true;
        pko.Keywords.Add("Yes");
        pko.Keywords.Add("No");
        pko.Keywords.Default = "No";


PromptResult pkr = ed.GetKeywords(pko);

if (pkr.Status != PromptStatus.OK)

return;

        eraseOrig = (pkr.StringResult == "Yes");

        pko.Message = "\nExplode MText?";
        pkr = ed.GetKeywords(pko);

if (pkr.Status != PromptStatus.OK)

return;

        explodeMText = (pkr.StringResult == "Yes");
      }


Transaction tr =
        db.TransactionManager.StartTransaction();

using (tr)
      {

// Collect our exploded objects in a single collection


DBObjectCollection objs = new
DBObjectCollection();


// Loop through the selected objects


foreach (SelectedObject so in psr.Value)
        {

// Open one at a time


Entity ent =
            (Entity)tr.GetObject(
              so.ObjectId,

OpenMode.ForRead
            );


// If dealing with MText...


MText mt = ent as
MText;

if (mt != null)
          {

// Create our callback method based on

// a lambda expression (C# 3.0 or higher)


MTextFragmentCallback cb =

new
MTextFragmentCallback(
                (frag, obj) =>
                {

// Great to see type inference working in C#, so

// IntelliSense treats frag as an MTextFragment

                  ed.WriteMessage(

"\nMText fragment found: {0}", frag.Text
                  );

return
MTextFragmentCallbackStatus.Continue;
                }
              );
            mt.ExplodeFragments(cb);
          }


// Only explode if not MText or we're also exploding

// MText objects


if (mt == null || explodeMText)
          {

// Explode the object into our collection


try
            {
              ent.Explode(objs);
            }

catch { }


// Erase the original, if requested


if (eraseOrig)
            {
              ent.UpgradeOpen();
              ent.Erase();
            }
          }
        }


// Now open the current space in order to

// add our resultant objects


BlockTableRecord btr =
          (BlockTableRecord)tr.GetObject(
            db.CurrentSpaceId,

OpenMode.ForWrite
          );


// Add each one of them to the current space

// and to the transaction


foreach (DBObject obj in objs)
        {

Entity ent = (Entity)obj;
          btr.AppendEntity(ent);
          tr.AddNewlyCreatedDBObject(ent, true);
        }


// And then we commit

        tr.Commit();
      }
    }
  }
}

I decided to use a lambda expression – introduced in C# 3.0 – to implement our callback function which then gets wrapped in a MTextFragmentCallback object. This will mean a little extra work if supporting prior versions of C# or converting the  code to VB.NET. I also added a try-catch block around the Explode() call, to pick up eNotApplicable exceptions and the like.
Let’s see what happens when we run the code, selecting the main MText object from the Sample/Mechanical Sample/Mechanical – Text and Tables.dwg sample drawing.
Here’s the selected MText – we can already see it contains a bunch of control codes from the contents property:

When we run the EXP command – choosing to both erase the original entities and explode MText (as well as the default behaviour of reporting the fragments) – we see these results on the command-line, with – quite importantly – not a control code in sight:
Command: EXP
Select objects to explode: 1 found
Select objects to explode:
Erase original objects? [Yes/No] <No>: Y
Explode MText? [Yes/No] <No>: Y
MText fragment found: GENERAL NOTES:
MText fragment found: 1.
MText fragment found: REFERENCE DRAWINGS ARE AS FOLLOWS:
MText fragment found: P & ID LEGEND
MText fragment found: SK-P-DR-0010
MText fragment found: GROUND LEVEL PLAN
MText fragment found: SK-F-DR-0001
MText fragment found: GENERAL NOTES ON CIVIL INTERFACE DRAWINGS
MText fragment found: SK-F-DR-0016
MText fragment found: STEAMFIELD LAYOUT
MText fragment found: SK-M-DR-0002
MText fragment found: SEPARATOR AREA - GENERAL ARRANGEMENT - PLAN
MText fragment found: SK-M-DR-0003
MText fragment found: SEPARATOR AREA - GENERAL ARRANGEMENT - ELEVATION
MText fragment found: SK-M-DR-0004
MText fragment found: PIPING KEY PLAN
MText fragment found: SK-M-DR-0010
MText fragment found: PIPING STANDARD DETAILS - CONDENSATE DRAIN POT
MText fragment found: SK-M-DR-0020
MText fragment found: PIPING STANDARD DETAILS - STEAM TRAP ASSEMBLY
MText fragment found: SK-M-DR-0021
MText fragment found: PIPING STANDARD DETAILS - HIGH POINT VENT
MText fragment found: SK-M-DR-0022
MText fragment found: PIPING STANDARD DETAILS - LOW POINT DRAIN
MText fragment found: SK-M-DR-0023
MText fragment found: PIPING STANDARD DETAILS - LOW POINT DRAIN (BRINE SYSTEM)
MText fragment found: SK-M-DR-0024
MText fragment found: PIPING STANDARD DETAILS - SAMPLE POINT
MText fragment found: STEAM AND CONDENSATE SERVICE
MText fragment found: SK-M-DR-0025
MText fragment found: PIPING STANDARD DETAILS - SAMPLE POINT
MText fragment found: CHEMICAL SERVICE
MText fragment found: SK-M-DR-0026
MText fragment found: PIPING STANDARD DETAILS -  ISOKINETIC SAMPLE PROBE
MText fragment found: SK-M-DR-0027
MText fragment found: PIPING STANDARD DETAILS -  PRESSURE TAPPING
MText fragment found:
MText fragment found: SK-M-DR-0028
MText fragment found: PIPING STANDARD DETAILS -  THERMOWELL
MText fragment found: SK-M-DR-0029
MText fragment found: PIPING STANDARD DETAILS -  ORIFICE PLATE
MText fragment found: SK-M-DR-0030
MText fragment found: PIPING STANDARD DETAILS -  SPRAY NOZZLE
MText fragment found:
MText fragment found: SK-M-DR-0031
MText fragment found: PIPING STANDARD DETAILS -  pH PROBE
MText fragment found: SK-M-DR-0032
MText fragment found: PIPING STANDARD DETAILS -  FLOW SWITCH
MText fragment found: SK-M-DR-0033
MText fragment found: PIPING STANDARD DETAILS -  SPRAY NOZZLE
MText fragment found: SK-M-DR-0034
MText fragment found: PIPING STANDARD DETAILS -  SPRAY NOZZLE
MText fragment found: SK-M-DR-0035
MText fragment found: PIPING STANDARD DETAILS -  STEAM ANALYZER
MText fragment found: SK-M-DR-0036
MText fragment found: INSULATION & CLADDING DETAILS
MText fragment found: SK-M-DR-0037, 0038 & 0039
MText fragment found: HP SEPARATOR - GENERAL NOTES AND NOZZLE LOADS
MText fragment found: SK-M-DR-0100
MText fragment found: HP SEPARATOR - GENERAL ARRANGEMENT
MText fragment found: SK-M-DR-0101
MText fragment found: HP SEPARATOR - INLET NOZZLE DETAIL
MText fragment found: SK-M-DR-0102
MText fragment found: HP SEPARATOR - INLET NOZZLE LEMNISCATE CURVE
MText fragment found: TEMPLATE DIMENSIONS
MText fragment found: SK-M-DR-0103
MText fragment found: LP SEPARATOR - GENERAL NOTES AND NOZZLE LOADS
MText fragment found: SK-M-DR-0110
MText fragment found: LP SEPARATOR - GENERAL ARRANGEMENT
MText fragment found: SK-M-DR-0111
MText fragment found: LP SEPARATOR - INLET NOZZLE DETAIL
MText fragment found: SK-M-DR-0112
MText fragment found: LP SEPARATOR - INLET NOZZLE LEMNISCATE CURVE
MText fragment found: TEMPLATE DIMENSIONS
MText fragment found: SK-M-DR-0113
MText fragment found: FLASH TANK - GENERAL ARRANGEMENT
MText fragment found: SK-M-DR-0120
MText fragment found: FLASH TANK - HOLD-DOWN BOLT GENERAL ARRANGEMENT
MText fragment found: SK-M-DR-0121
MText fragment found: FLASH TANK - WEIR BOX DETAILS
MText fragment found: SK-M-DR-0122
MText fragment found: FLASH TANK - NOZZLE DETAILS
MText fragment found: SK-M-DR-0123
MText fragment found: FLASH TANK - OTHER NOZZLE DETAILS
MText fragment found: SK-M-DR-0124
MText fragment found: FLASH TANK - BAFFLE PLATE DETAILS
MText fragment found: SK-M-DR-0125
MText fragment found: ACID STORAGE TANK - GENERAL ARRANGEMENT
MText fragment found: SK-M-DR-0130
MText fragment found: ACID STORAGE TANK - HOLD-DOWN BOLT GENERAL ARRANGEMENT
MText fragment found: SK-M-DR-0131
MText fragment found: ACID STORAGE TANK - NOZZLE DETAILS
MText fragment found: SK-M-DR-0132
MText fragment found: PIPE SUPPORTS - PIPE GUIDE TYPE PG1
MText fragment found: SK-M-DR-0250
MText fragment found: PIPE SUPPORTS - PIPE GUIDE TYPE PG2
MText fragment found: SK-M-DR-0251
MText fragment found: PIPE SUPPORTS - PIPE GUIDE TYPE PG3
MText fragment found: SK-M-DR-0252
MText fragment found: PIPE SUPPORTS - LINE STOP TYPE LS1
MText fragment found: SK-M-DR-0253
MText fragment found: PIPE SUPPORTS - LINE STOP TYPE LS2
MText fragment found: SK-M-DR-0254
MText fragment found: PIPE SUPPORTS - LINE STOP TYPE LS3
MText fragment found: SK-M-DR-0255
MText fragment found: PIPE SUPPORTS - PIPE ANCHOR TYPE PA1
MText fragment found: SK-M-DR-0256
MText fragment found: PIPE SUPPORTS - PIPE ANCHOR TYPE PA2
MText fragment found: SK-M-DR-0257
MText fragment found: PIPE SUPPORTS - PIPE ANCHOR TYPE PA3
MText fragment found: SK-M-DR-0258
MText fragment found: 2.
MText fragment found: PIPING SHALL BE IN ACCORDANCE WITH THE FOLLOWING
SPECIFICATIONS:
MText fragment found: PIPE SPECIFICATION - CS TWO PHASE
MText fragment found: - XXXX
MText fragment found: SK-M-SP-0001
MText fragment found: PIPE SPECIFICATION - CS HP STEAM
MText fragment found: - XXXX
MText fragment found: SK-M-SP-0002
MText fragment found: PIPE SPECIFICATION - CS LP STEAM
MText fragment found: - XXXX
MText fragment found: SK-M-SP-0003
MText fragment found: PIPE SPECIFICATION - CS BRINE
MText fragment found:
MText fragment found: - XXXX
MText fragment found: SK-M-SP-0004
MText fragment found: PIPE SPECIFICATION - HDPE CONDENSATE
MText fragment found: - XXXX
MText fragment found: SK-M-SP-0005
MText fragment found: PIPE SPECIFICATION - SS CONDENSATE
MText fragment found: - XXXX
MText fragment found: SK-M-SP-0006
MText fragment found: PIPE SPECIFICATION - SS WASH WATER
MText fragment found: - XXXX
MText fragment found: SK-M-SP-0007
MText fragment found: PIPE SPECIFICATION - HASTELLOY ACID
MText fragment found: - XXXX
MText fragment found: SK-M-SP-0008
MText fragment found: 3.
MText fragment found: INSPECTION AND TESTING PIPING SHALL BE INSPECTED AND
TESTED IN ACCORDANCE  WITH THE SPECIFIED
MText fragment found: CODE AS WELL AS THE FOLLOWING:
MText fragment found: A.
MText fragment found: 100% RADIOGRAPH FOR PRESSURE RETAINING WELDS
MText fragment found: B.
MText fragment found: ALL NON PRESSURE WELDS TO BE MAG PARTICLE OR DYE
PENETRANT TESTED
MText fragment found: C.
MText fragment found: HYDROSTATIC TEST PRESSURE IN ACCORDANCE WITH ASME
MText fragment found: VIII DIVISION 1  PARAGRAPH UG-99(C)
MText fragment found: D.
MText fragment found: THE CONTRACTOR IS TO PROVIDE ANY BLIND FLANGES AND
EQUIPMENT TO  UNDERTAKE THE
MText fragment found: TESTING
MText fragment found: REFER TO SPECIFICATION FOR HYDROSTATIC TESTING OF PIPES
MText fragment found: SK-M-SP-0024
MText fragment found: 4.
MText fragment found: PIPING LAYOUT DIMENSIONS ARE SHOWN IN MILLIMETERS, FOR
THE PIPING &  WELLHEADS IN THE COLD
MText fragment found: POSITION. GRIDLINE COORDINATES AND ELEVATIONS ARE  IN
METERS. PIPE DIAMETERS ARE SHOWN IN
MText fragment found: INCHES.
MText fragment found: 5.
MText fragment found: ELEVATIONS ARE GIVEN TO PIPE CENTERLINES UNLESS OTHERWISE
NOTED.
MText fragment found: 6.
MText fragment found: ABBREVIATIONS:
MText fragment found: BE
MText fragment found: BEVELED ENDS
MText fragment found: O/D
MText fragment found: OUTSIDE DIAMETER
MText fragment found: BW
MText fragment found: BUTT WELD
MText fragment found: PBE
MText fragment found: PLAIN BOTH ENDS
MText fragment found: CDP
MText fragment found: CONDENSATE DRAIN POINT
MText fragment found: PFC
MText fragment found: PARALLEL FLANGED CHANNEL
MText fragment found: CS
MText fragment found: CARBON STEEL
MText fragment found: PL
MText fragment found: PLATE
MText fragment found: C/WT
MText fragment found: COUNTER WEIGHT
MText fragment found: POE
MText fragment found: PLAIN ONE END
MText fragment found: CENTER LINE
MText fragment found: PTA
MText fragment found: PRESSURE TAPPING
MText fragment found: CTRS
MText fragment found: CENTERS
MText fragment found: RF
MText fragment found: RAISED FACE
MText fragment found: CVR
MText fragment found: COVER
MText fragment found: RL
MText fragment found: REDUCED LEVEL
MText fragment found: COS
MText fragment found: CHECK ON SITE
MText fragment found: RO
MText fragment found: RESTRICTION ORIFICE
MText fragment found: C
MText fragment found: CHANNEL
MText fragment found: RSA
MText fragment found: ROLLED STEEL ANGLE
MText fragment found: E
MText fragment found: EAST
MText fragment found: RTJ
MText fragment found: RING TYPE JOINT
MText fragment found: EW
MText fragment found: EACH WAY
MText fragment found: SPA
MText fragment found: STEAM SAMPLING POINT
MText fragment found: FO
MText fragment found: FLOW ORIFICE
MText fragment found: STD
MText fragment found: STANDARD
MText fragment found: GALV
MText fragment found: GALVANIZED
MText fragment found: SAW
MText fragment found: SUBMERGED ARC WELDED
MText fragment found: HPV
MText fragment found: HIGH POINT VENT
MText fragment found: SMLS
MText fragment found: SEAMLESS
MText fragment found: IAC
MText fragment found: INSTRUMENT AIR CONNECTION
MText fragment found: SP
MText fragment found: SAMPLE  POINT
MText fragment found: IP
MText fragment found: INTERSECTION POINT
MText fragment found: SW
MText fragment found: SOCKET WELD
MText fragment found: IS
MText fragment found: ISOKINETIC SAMPLE POINT
MText fragment found: TBE
MText fragment found: THREADED BOTH ENDS
MText fragment found: L
MText fragment found: ANGLE
MText fragment found: THD
MText fragment found: THREADED
MText fragment found: LG
MText fragment found: LONG
MText fragment found: TOE
MText fragment found: THREADED ONE END
MText fragment found: LPD
MText fragment found: LOW POINT DRAIN
MText fragment found: TOS
MText fragment found: TOP OF STEEL
MText fragment found: LR
MText fragment found: LONG RADIUS
MText fragment found: TOG
MText fragment found: TOP  OF GRATING
MText fragment found: MDFT
MText fragment found: MINIMUM DRY FILM THICKNESS
MText fragment found: TWA
MText fragment found: THERMOWELL SCREWED
MText fragment found: MS
MText fragment found: MILD STEEL
MText fragment found: TWB
MText fragment found: THERMOWELL FLANGED
MText fragment found: N
MText fragment found: NORTH
MText fragment found: THK
MText fragment found: THICK
MText fragment found: No
MText fragment found: NUMBER
MText fragment found: TYP
MText fragment found: TYPICAL
MText fragment found: NOM
MText fragment found: NOMINAL
MText fragment found: WN
MText fragment found: WELD NECK
MText fragment found: NPS
MText fragment found: NOMINAL PIPE SIZE
MText fragment found: WT
MText fragment found: WALL THICKNESS
MText fragment found: CLASS
MText fragment found: 7.
MText fragment found: PIPE SUPPORT TYPES:
MText fragment found: PA
MText fragment found: FIXED ANCHOR
MText fragment found: PG
MText fragment found: GUIDE
MText fragment found: PC
MText fragment found: CONSTANT WEIGHT SUPPORT
MText fragment found: PS
MText fragment found: SLIDE
MText fragment found: 8.
MText fragment found: WHERE APPLICABLE DIMENSIONS SHOWN ON LONG SECTIONS ARE
PLAN LENGTHS.
MText fragment found: 9.
MText fragment found: ALL GASKETS ARE 3.2mm THICK (COMPRESSED) UNLESS NOTED
OTHERWISE.
MText fragment found: 10.
MText fragment found: ALL FITTING TO FITTING DIMENSIONS INCLUDE 3mm WELD GAPS.
MText fragment found: 11.
MText fragment found: CONTOURS REFLECT EXISTING GROUND LEVELS. WHERE APPLICABLE
REFER LONG SECTION  FOR DESIGN
MText fragment found: GROUND LEVEL. CONFIRM LEVELS ON SITE.
MText fragment found: 12.
MText fragment found: INTERSECTION POINTS (IP's) SHALL BE PEGGED OUT AND
VERIFIED BY COMPANY BEFORE SUPPORT OR
MText fragment found: PIPING INSTALLATION CAN COMMENCE.
MText fragment found: 13.
MText fragment found: MAXIMUM MISALIGNMENT OF ANY SUPPORT FROM A STRAIGHT LINE
BETWEEN I.P.'s HORIZONTAL  ±10mm
MText fragment found: VERTICAL      ±3mm
MText fragment found: 14.
MText fragment found: INSULATION SHALL BE IN ACCORDANCE WITH THE FOLLOWING
SPECIFICATION AND DRAWINGS:
MText fragment found: SPECIFICATION
MText fragment found: SK-M-SP-0036
MText fragment found: DRAWINGS
MText fragment found: SK-M-DR-0037, 0038, 0039
MText fragment found: 15.
MText fragment found: CONSTANT WEIGHT SUPPORT TO BE INSTALLED HORIZONTAL IN THE
COLD POSITION (BOTH  PIPING AND
MText fragment found: WELL) UNLESS NOTED OTHERWISE.
MText fragment found: 16.
MText fragment found: CHANGES IN DIRECTION OR SLOPE TO BE FULL OR TRIMMED
ELBOWS UNLESS SINGLE OR DOUBLE  MITER
MText fragment found: BENDS ARE SPECIFIED. FOR NITRE BEND DETAILS SEE DRAWING
LISTED IN NOTE 1 ABOVE.
MText fragment found: 17.
MText fragment found: CONTRACTOR SHALL SUBMIT CONSTRUCTION PLAN AND METHODOLOGY
FOR EACH AREA  (WELL PAD, CROSS
MText fragment found: COUNTRY ROUTE, TIE-IN, ETC.) TO COMPANY FOR REVIEW PRIOR
TO  COMMENCING WORK.
MText fragment found: 18.
MText fragment found: CONTRACTOR SHALL SUBMIT HYDROTEST PLAN AND METHODOLOGY
FOR EACH AREA TO  COMPANY FOR
MText fragment found: REVIEW PRIOR TO TESTING.

Selecting the resultant objects, we can see that – while we now have two – they are still MText:

If we use EXP on these two objects – selecting the same options as before – we finally see the MText decomposed into DBText objects (there’s no need to show the command-line results again – it’ll be the same as last time, with the minor difference of us selecting two objects instead of one). I’ve only selected the text in the first column – if we select all 133 objects in both columns, AutoCAD won’t show the grip markers, which makes it less clear they are now all separate objects:

Thanks for the interesting suggestion, Mike! :-)

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
"觉得好,就打赏"
还没有人打赏,支持一下

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-12-24 21:58 , Processed in 0.173532 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表