By Deepak Nadig
Recently, an ADN partner requested for a method to check if a text is contained in a Linetype before extracting the text.
To extract a text, LinetypeTableRecord.TextAt is used and it returns eNotApplicable error when text is not present at index in the record. To avoid this, null id check using LinetypeTableRecord.ShapeStyleAt can be used. This method returns null, if text is not present at an index in the LinetypeTableRecord.
Here is a quick sample to check if text is present prior to extraction of the text :
[CommandMethod("lineTypeText")] public void lineTypeText() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Transaction tr = db.TransactionManager.StartTransaction(); Editor ed = doc.Editor; using (tr) { DBObject tmpObj = tr.GetObject(db.LinetypeTableId, OpenMode.ForRead); LinetypeTable pLineTable = (LinetypeTable)tmpObj; if ((pLineTable != null)) { foreach (ObjectId tblRecId in pLineTable) { LinetypeTableRecord pLineType = (LinetypeTableRecord)tr.GetObject(tblRecId, OpenMode.ForRead); if ((pLineType != null)) { for (int dash = 0; dash < pLineType.NumDashes; dash++) { /* LinetypeTableRecord.ShapeStyleAt returns the ObjectId of the TextStyleTableRecord of the shape (or textStyle if it's a text string instead of a shape) at position index in the LinetypeTableRecord. If there is no shape or text at index, then Null is returned.*/ ObjectId objIdShape = pLineType.ShapeStyleAt(dash); if (objIdShape != ObjectId.Null) { string pText = ""; pText = pLineType.TextAt(dash); if (pText == "") { ed.WriteMessage("no dash text "); } else { ed.WriteMessage("dash :" + dash.ToString()); ed.WriteMessage("\ndash text :" + pText); } } } } } } } }
The output generated by this code snippet is as seen in this screenshot :
Update: The above code is not equipped to handle shape\symbol in linetype. You can use the LinetypeTableRecord.ShapeNumberAt API to determine if shape exists. It returns 0 if shape does not exist. Below is the modified code that Can be used for testing :
[CommandMethod("lineTypeText")] public void lineTypeText() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Transaction tr = db.TransactionManager.StartTransaction(); Editor ed = doc.Editor; using (tr) { DBObject tmpObj = tr.GetObject(db.LinetypeTableId, OpenMode.ForRead); LinetypeTable pLineTable = (LinetypeTable)tmpObj; if ((pLineTable != null)) { foreach (ObjectId tblRecId in pLineTable) { LinetypeTableRecord pLineType = (LinetypeTableRecord)tr.GetObject(tblRecId, OpenMode.ForRead); if ((pLineType != null)) { for (int dash = 0; dash < pLineType.NumDashes; dash++) { ObjectId objIdShape = pLineType.ShapeStyleAt(dash); //If there is no shape or text at index, then Null is returned. int shpNum = pLineType.ShapeNumberAt(dash); if (objIdShape != ObjectId.Null) { ed.WriteMessage("text or shape exist;"); string pText = ""; //returns 0 if there is no shape\symbol at the index, which means text exist in this context if (shpNum == 0) { pText = pLineType.TextAt(dash); ed.WriteMessage(" dash text :" + pText ); } ed.WriteMessage("\n"); } } } } } } }