Author Topic: Programming Fairies needed  (Read 10832 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Programming Fairies needed
« on: April 19, 2007, 07:03:51 AM »
A couple of centuries ago, when I was on the board, we used to leave biccies out at night for the drafting fairies in the hope that they'd finish our drawings ... or at least not change dimension on the tracing left on the board.

I'm sort of hoping the programming fairies see this ...

Getting the Dimension Text value requires a bit of hoop jumping in Lisp .. I think I've posted a couple of examples here. The process requires diving into nested DXF definitions.

Has anyone played with extracting the actual TextValue from dimensions in the Net API or ObjectARX.

Here's why ..

Code: [Select]
[CommandMethod("SANNO_1")]
public void SelectAnnotation()
{
    Document doc = AcadApp.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;
    PromptSelectionOptions selectionOptions = new PromptSelectionOptions();
    TypedValue[] filterList = { new TypedValue((int)DxfCode.Start, "MText,Text,Dimension") };
    SelectionFilter filter = new SelectionFilter(filterList);
    PromptSelectionResult selectionResult = ed.GetSelection(selectionOptions, filter);

    //Do nothing if selection is unsuccessful
    if (selectionResult.Status != PromptStatus.OK)
        return;
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        try
        {
            ObjectId[] objIds = selectionResult.Value.GetObjectIds();
            foreach (ObjectId objId in objIds)
            {
                DBObject dbObj = (DBObject)tr.GetObject(objId, OpenMode.ForRead);
                string typeName = dbObj.GetType().Name; // temp for testing
                string textValue;
                ed.WriteMessage("\nDBObject.GetType() : "
                                + dbObj.GetType());
                switch (dbObj.GetType().Name)
                {
                    case "MText":
                        // probably should use ExplodeFragments here !!
                        // with MTextFragmentCallback
                        MText mText = (MText)dbObj;
                        textValue = mText.Text;
                        ed.WriteMessage("\nWe have Mtext:- Text Property = "
                                        + textValue);
                        break;
                    case "DBText":                               
                        DBText dText = (DBText)dbObj;
                        textValue = dText.TextString;
                        ed.WriteMessage("\nWe selected Text:- TextString Property = "
                                        + textValue);
                        break;
                     case "AlignedDimension":                             
                     case "RotatedDimension":
                     case "ArcDimension":                               
                        Dimension dimObj = (Dimension)dbObj;
                        // THIS NEEDS TO BE RESOLVED ..LEAVE FOR THE PROGRAMMING FAIRIES.
                        textValue = dimObj.DimensionText;
                        ed.WriteMessage("\nWe selected a Dimension :- DimensionText = "
                                        + textValue);
                        break;                               
                }
            }
        }
        catch (Autodesk.AutoCAD.Runtime.Exception ex)
        {
            ed.WriteMessage(ex.Message);
            tr.Abort();
        }
    }
}


.. and the obligatory piccy.


kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Chuck Gabriel

  • Guest
Re: Programming Fairies needed
« Reply #1 on: April 19, 2007, 08:04:38 AM »
This is C++, but I think you'll get the drift:

Code: [Select]
if(acdbOpenObject(pDim, objId, AcDb::kForWrite) == Acad::eOk) {
char* pszDimText = pDim->dimensionText();
string dimText(pszDimText);
delete[] pszDimText;
pszDimText = NULL;
if(dimText.length() > 0) {
stackText(dimText);
pDim->setDimensionText(dimText.c_str());
}
else {
dimText = textFromMeasurement(pDim);
stackText(dimText);
pDim->setDimensionText(dimText.c_str());
}
pDim->close();
}

Code: [Select]
string textFromMeasurement(AcDbDimension* pDim) {
double dblMeasurement = 0;
pDim->measurement(dblMeasurement);
char* pszDimText = new char[26];
acdbRToS(dblMeasurement,
curDoc()->database()->dimlunit(),
curDoc()->database()->dimdec(),
pszDimText);
string retVal(pszDimText);
delete[] pszDimText;
pszDimText = NULL;
return retVal;
}

Draftek

  • Guest
Re: Programming Fairies needed
« Reply #2 on: April 19, 2007, 08:11:24 AM »
hmmm... That's odd because -
Here is something I used nested in a loop that works okay from a selection set:

Code: [Select]
Dimension oDim = (Dimension)trans.GetObject(oSS[i].ObjectId, OpenMode.ForWrite);
double dVal = oDim.Measurement;
string iValue = Math.Round(dVal, 4).ToString();
// convert to milimeters and then round to 2 decimal places
dVal = Math.Round(oCon.InchToMM(dVal), 2);
ModifyBlockAtt(oDim.DimensionText, dVal.ToString(), iValue);

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Programming Fairies needed
« Reply #3 on: April 19, 2007, 08:14:59 AM »
This is C++, but I think you'll get the drift:

Code: [Select]
...............


So you basically reconstruct it from the Measurement Property and dimVars Chuck, yes ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Chuck Gabriel

  • Guest
Re: Programming Fairies needed
« Reply #4 on: April 19, 2007, 08:17:25 AM »
This is C++, but I think you'll get the drift:

Code: [Select]
...............


So you basically reconstruct it from the Measurement Property and dimVars Chuck, yes ?

Yes sir!  IIRC, if there is no override text, DimensionText returns a null string and you have to derive the dimension text from the measurement value.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Programming Fairies needed
« Reply #5 on: April 19, 2007, 08:17:44 AM »
This is how I do it in Lisp
Code - Auto/Visual Lisp: [Select]
  1. ;;;------------------------------------------------------------------
  2. ;;;------------------------------------------------------------------
  3. ;;;
  4. (defun KDUB:getdimensiontext (DimEnt / anon_block nested_ent returnval)
  5.   (if (and (or (= (type DimEnt) 'ENAME) (setq DimEnt (vlax-vla-object->ename DimEnt)))
  6.            (setq dent (entget DimEnt '("*")))
  7.            (= "DIMENSION" (cdr (assoc 0 dent)))
  8.            (setq anon_block (cdr (assoc 2 dent)))
  9.            (setq nested_ent (cdr (assoc -2 (tblsearch "BLOCK" anon_block 0))))
  10.       )
  11.     (while (and (not returnval) (setq nested_ent (entnext nested_ent)))
  12.       (if (= "MTEXT" (cdr (assoc 0 (entget nested_ent))))
  13.         (setq returnval nested_ent)
  14.       )
  15.     )
  16.   )
  17.   ;; Return the Mtext Object
  18.   returnval
  19. )
« Last Edit: March 23, 2012, 06:07:37 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Programming Fairies needed
« Reply #6 on: April 19, 2007, 08:19:53 AM »

DrafteK, you reconstruct yours too .. yes ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Programming Fairies needed
« Reply #7 on: April 19, 2007, 08:48:14 AM »
Just had a look .. looks like all the DimVAr properties are stored with the Dimension.

So shouldn't be too hard to reconstruct the DimensionText Value .. sort of ..
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

LE

  • Guest
Re: Programming Fairies needed
« Reply #8 on: April 19, 2007, 01:30:30 PM »
Haber maestro Kerry, ve si esto te puede servir:

Code: [Select]
[CommandMethod("DIMMTEXT")]
public void dimmtext()
{
    Document doc = acadApp.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;
    Database db = doc.Database;
    PromptEntityResult res = ed.GetEntity("\nSelect dimension: ");
    if (res.Status != PromptStatus.OK) return;
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        Dimension dim = tr.GetObject(res.ObjectId, OpenMode.ForRead, false) as Dimension;
        if (dim != null)
        {
            ed.WriteMessage(dim.GetType().Name); //test

            String str = dim.DimensionText;
            if (str == "")
            {
                str = Math.Round(dim.Measurement, dim.Dimtdec).ToString();
            }
            else if (str.Contains("<>"))
            {
                str = Math.Round(dim.Measurement, dim.Dimtdec).ToString();
                str = "Has been overridden<> Measurement=" + str;
            }

            //if (dim.GetType().Name.Contains("LineAngularDimension2")) // no workie
            //{
            //    double d = Math.Round(dim.Measurement, dim.Dimtdec);
            //    double ang = d / Math.PI * 180.0;
            //    ed.WriteMessage(Converter.AngleToString(ang));
            //    //ed.WriteMessage("\n", Converter.AngleToString(d, AngularUnitFormat.Grads, -1));
            //}

            ed.WriteMessage("\nDimension text [{0}]", str);
        }
        tr.Commit();
    }
}

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Programming Fairies needed
« Reply #9 on: April 20, 2007, 08:50:55 AM »
I'm reviewing the way I've done the case selection.

Currently I'm using a string (the Class Type) to test against
ie:
Code: [Select]
                switch (dbObj.GetType().Name)
                {
                    case "MText":
                     //............

I have it in my mind that the Entity types are enumerated somewhere, but can't find it.

Anyone have any ideas, or a suggestion for more economical way to test other than using strings.



ps: thanks to the sprites who have responded so far.. :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Programming Fairies needed
« Reply #10 on: April 20, 2007, 09:25:34 AM »
Kerry, is there any way to find the name of the dim block?

Glenn R

  • Guest
Re: Programming Fairies needed
« Reply #11 on: April 20, 2007, 09:31:03 AM »
if(dimObj.GetType() == typeof(Dimension))
  // Do something interesting here with a dimension

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Programming Fairies needed
« Reply #12 on: April 20, 2007, 09:32:05 AM »
I'm going to have a look over the weekend Bryco ... unless someone who knows pops is and shouts beforehand.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

TonyT

  • Guest
Re: Programming Fairies needed
« Reply #13 on: April 20, 2007, 10:11:07 AM »
if(dimObj.GetType() == typeof(Dimension))
  // Do something interesting here with a dimension

Um... don't you mean:

  if( dimObj is Dimension )

Dimension is an abstract class, and the base
class of all concrete dimension classes, but
the expression:

     obj.GetType() == typeof( Dimension )

is true only if obj is an instance of a Dimension,
but false if it is an insance of a class derived
from same.

So, to tell if a given object is an instance of
any class derived from Dimension, you woud
need to use

    if( obj is Dimension )

« Last Edit: April 20, 2007, 03:51:40 PM by TonyT »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Programming Fairies needed
« Reply #14 on: April 20, 2007, 09:43:39 PM »
DUH !!
I'm going to have a look over the weekend Bryco ... unless someone who knows pops is and shouts beforehand.
As usual, the trick is  RTFM

Quote
DBObject.GetType() : Autodesk.AutoCAD.DatabaseServices.MText
Text Property = Hello Mtext World

DBObject.GetType() : Autodesk.AutoCAD.DatabaseServices.ArcDimension
DimensionText = 1045 , Measurement = 1045.02008357775

DBObject.GetType() : Autodesk.AutoCAD.DatabaseServices.AlignedDimension
DimensionText = 566 , Measurement = 566.212198720329

DBObject.GetType() : Autodesk.AutoCAD.DatabaseServices.RotatedDimension
DimensionText = 676 , Measurement = 676.204852354089

DBObject.GetType() : Autodesk.AutoCAD.DatabaseServices.MText
Text Property = Hello Mtext World

DBObject.GetType() : Autodesk.AutoCAD.DatabaseServices.DBText
We selected Text:- TextString Property = Hello Text World

DBObject.GetType() : Autodesk.AutoCAD.DatabaseServices.RotatedDimension
DimensionText = 1338 , Measurement = 1338.13021112601

DBObject.GetType() : Autodesk.AutoCAD.DatabaseServices.RotatedDimension
DimensionText = 457 , Measurement = 456.727064654551
« Last Edit: April 20, 2007, 10:18:15 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.