Last active
April 11, 2018 11:36
-
-
Save MadhukarMoogala/2fa757915bae769a6e54bfccae21e7d6 to your computer and use it in GitHub Desktop.
Explode To Text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void EXPTXT() | |
{ | |
var doc = Application.DocumentManager.MdiActiveDocument; | |
var db = doc.Database; | |
var ed = doc.Editor; | |
// selection text | |
ObjectId[] dbtextIds = ed.SelectTextEntitesInModelSpace(); | |
foreach (ObjectId id in dbtextIds) | |
{ | |
using (Transaction tr = db.TransactionManager.StartTransaction()) | |
{ | |
var text = (DBText)tr.GetObject(id, OpenMode.ForWrite); | |
ObjectId[] ids = new ObjectId[1]; | |
ids[0] = id; | |
ed.SetImpliedSelection(ids); | |
var tempFile = Path.Combine(Path.GetTempPath(), "Q.wmf"); | |
if (File.Exists(tempFile)) | |
{ | |
File.Delete(tempFile); | |
} | |
ed.Command("_.WMFOUT", tempFile, "", ""); | |
var viewSize = (double)Application.GetSystemVariable("VIEWSIZE"); | |
var screenSize = (Point2d)Application.GetSystemVariable("SCREENSIZE"); | |
double factor = viewSize * (screenSize.X / screenSize.Y); | |
var viewCtr = (Point3d)Application.GetSystemVariable("VIEWCTR"); | |
//Transform viewCtr from UCS to DCS | |
Matrix3d matUCS2DCS = ed.UCS2WCS() * ed.WCS2DCS(); | |
viewCtr = viewCtr.TransformBy(matUCS2DCS); | |
var p1 = new Point3d(viewCtr.X - (factor / 2.0), viewCtr.Y - (viewSize / 2.0), .0); | |
var p2 = new Point3d(viewCtr.X + (factor / 2.0), viewCtr.Y + (viewSize / 2.0), .0); | |
//Transorm p1,p2 from DCS to UCS; | |
Matrix3d matDCS2UCS = ed.DCS2WCS() * ed.WCS2UCS(); | |
p1 = p1.TransformBy(matDCS2UCS); | |
p2 = p2.TransformBy(matDCS2UCS); | |
Point2d wmfinBlockPos = new Point2d(p1.X, p2.Y); | |
var tempWithOutExt = Path.Combine(Path.GetDirectoryName(tempFile), | |
Path.GetFileNameWithoutExtension(tempFile)); | |
ed.Command("_.WMFIN", tempWithOutExt, wmfinBlockPos, "2", "", ""); | |
try | |
{ | |
var wmfBlock = tr.GetObject(ed.SelectLastEnt(), OpenMode.ForWrite) as BlockReference; | |
DBObjectCollection pElems = new DBObjectCollection(); | |
wmfBlock?.Explode(pElems); | |
var space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); | |
foreach (DBObject elem in pElems) | |
{ | |
space.AppendEntity(elem as Entity); | |
tr.AddNewlyCreatedDBObject(elem, true); | |
} | |
//Purge unused WMFIN Block and reference | |
ObjectId wmfBtr = GetNonErasedTableRecordId(db.BlockTableId, wmfBlock.Name); | |
ObjectIdCollection blockIds = new ObjectIdCollection(); | |
blockIds.Add(wmfBtr); | |
db.Purge(blockIds); | |
foreach (ObjectId oId in blockIds) | |
{ | |
DBObject obj = tr.GetObject(oId, OpenMode.ForWrite); | |
obj.Erase(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
ed.WriteMessage(ex.Message); | |
} | |
finally | |
{ | |
//Erase text entity | |
text.Erase(); | |
tr.Commit(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment