Author Topic: Programming Fairies needed  (Read 10900 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Programming Fairies needed
« Reply #15 on: April 20, 2007, 10:09:03 PM »
I'll update later  ... but in the meantime ;

Code: [Select]
string textValue = dimObj.FormatMeasurement(dimObj.Measurement, "");
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 #16 on: April 20, 2007, 10:51:49 PM »
Still need some work, but ....
Code - C#: [Select]
  1. // codehimbelonga kwb@theSwamp:20070421
  2.  
  3. [CommandMethod("SUNNY_1")]
  4. public void SelectAnnotation()
  5. {
  6.     Document doc = AcadApp.DocumentManager.MdiActiveDocument;
  7.     Database db = doc.Database;
  8.     Editor ed = doc.Editor;
  9.     PromptSelectionOptions selectionOptions = new PromptSelectionOptions();
  10.     TypedValue[] filterList =   { new TypedValue((int)DxfCode.Start, "MText,Text,Dimension") };
  11.     SelectionFilter filter = new SelectionFilter(filterList);
  12.     PromptSelectionResult selectionResult = ed.GetSelection(selectionOptions, filter);
  13.  
  14.     //Do nothing if     selection is unsuccessful
  15.     if (selectionResult.Status != PromptStatus.OK)
  16.         return;
  17.     using (Transaction tr = db.TransactionManager.StartTransaction())
  18.     {
  19.         try
  20.         {
  21.             ObjectId[] objIds = selectionResult.Value.GetObjectIds();
  22.             string textValue;
  23.             foreach (ObjectId objId in objIds)
  24.             {
  25.                 Entity ent = tr.GetObject(objId, OpenMode.ForRead) as Entity ;
  26.                 if (ent == null) return;
  27.  
  28.                 ed.WriteMessage("\nDBObject.GetType() : " + ent.GetType().Name);                        
  29.                 switch (ent.GetType().Name)
  30.                 {
  31.                     case "MText":                            
  32.                         // probably should use ExplodeFragments here !!
  33.                         // with MTextFragmentCallback
  34.                         MText mText = (MText)ent;
  35.                         textValue = mText.Text;
  36.                         ed.WriteMessage("\n Text Property = "
  37.                                         + textValue);
  38.                         break;
  39.                     case "DBText":                            
  40.                         DBText dText = (DBText)ent;
  41.                         textValue = dText.TextString;
  42.                         ed.WriteMessage("\n TextString Property = "
  43.                                         + textValue);
  44.                         break;
  45.                     case "AlignedDimension":
  46.                     case "RotatedDimension":
  47.                     case "ArcDimension":
  48.                         Dimension dimObj = (Dimension)ent;                                
  49.                         if (dimObj.DimensionText.Length > 0)
  50.                             textValue = dimObj.DimensionText;
  51.                         else                                                              
  52.                             textValue = dimObj.FormatMeasurement(dimObj.Measurement, "");
  53.  
  54.                         ed.WriteMessage("\n DimensionText = "
  55.                                         + textValue
  56.                                         + " , Measurement = "
  57.                                         + dimObj.Measurement
  58.                                         );
  59.                         break;
  60.                 }
  61.             }
  62.         }
  63.         catch (Autodesk.AutoCAD.Runtime.Exception ex)
  64.         {
  65.             ed.WriteMessage(ex.Message);
  66.             tr.Abort();
  67.         }
  68.     }
  69. }

... and the piccy;
« Last Edit: March 23, 2012, 06:06:41 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.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Programming Fairies needed
« Reply #17 on: April 20, 2007, 11:54:03 PM »
Kerry that is looking good but some pretty hard yacker to get what you need, I have to agree with the bloke who said net was for professions. If I get there  it will be only be by following in someones footsteps. Lucky there's some big feet in the swamp.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Programming Fairies needed
« Reply #18 on: April 21, 2007, 12:01:35 AM »
Bryco, I have clodhoppers, but not "big feet" :-)

Due to the nature and age of the beast there is not yet a lot of good sample stuff around .. most of it is hacked like this is.

Hopefully people here will continue/start posting their discoveries and assist in helping us ALL grow.

This particular code is perhaps more expansive than it needs to be just to show the dimension bits.

be well
and thanks for the comments.



added ps:
I'm looking forward to CharlesAlanB. to start with the C# stuff ...
with his sense of adventure and tenacity it should be fun.



« Last Edit: April 21, 2007, 12:57:08 AM 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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Programming Fairies needed
« Reply #19 on: April 21, 2007, 12:51:37 AM »
.. this was where I was heading,  .. still pretty primitive though ..
Code - Auto/Visual Lisp: [Select]
  1. // codehimbelonga kwb@theSwamp:20070421
  2.  
  3. [CommandMethod("SUNNY_2")]
  4. public void SelectAnnotation2()
  5. {
  6.     Document doc = AcadApp.DocumentManager.MdiActiveDocument;
  7.     Database db = doc.Database;
  8.     Editor ed = doc.Editor;
  9.     PromptSelectionOptions selectionOptions = new PromptSelectionOptions();
  10.     TypedValue[] filterList =   { new TypedValue((int)DxfCode.Start, "MText,Text,Dimension") };
  11.     SelectionFilter filter = new SelectionFilter(filterList);
  12.     PromptSelectionResult selectionResult = ed.GetSelection(selectionOptions, filter);
  13.  
  14.     //Do nothing if     selection is unsuccessful
  15.     if (selectionResult.Status != PromptStatus.OK)
  16.         return;
  17.  
  18.     List<Double> numValues = new List<Double>();
  19.     string textValue;
  20.     double numValue = 0.0;
  21.     using (Transaction tr = db.TransactionManager.StartTransaction())
  22.     {
  23.         try
  24.         {
  25.             ObjectId[] objIds = selectionResult.Value.GetObjectIds();
  26.  
  27.             foreach (ObjectId objId in objIds)
  28.             {
  29.                 Entity ent = tr.GetObject(objId, OpenMode.ForRead) as Entity;
  30.                 if (ent == null) return;
  31.  
  32.                 ed.WriteMessage("\nEntity.GetType() : " + ent.GetType().Name);
  33.                 switch (ent.GetType().Name)
  34.                 {
  35.                     case "MText":
  36.                         // probably should use ExplodeFragments here !!
  37.                         // with MTextFragmentCallback
  38.                         MText mText = (MText)ent;
  39.                         textValue = mText.Text;
  40.                         ed.WriteMessage("\n Text Property = "
  41.                                         + textValue);
  42.                         if (kdubUtils.isNumeric(textValue))
  43.                             numValues.Add(Convert.ToDouble(textValue));
  44.                         break;
  45.                     case "DBText":
  46.                         DBText dText = (DBText)ent;
  47.                         textValue = dText.TextString;
  48.                         ed.WriteMessage("\n TextString Property = "
  49.                                         + textValue);
  50.                         if (kdubUtils.isNumeric(textValue))
  51.                             numValues.Add(Convert.ToDouble(textValue));
  52.                         break;
  53.                     case "AlignedDimension":
  54.                     case "RotatedDimension":
  55.                     case "ArcDimension":
  56.                         Dimension dimObj = (Dimension)ent;
  57.                         if (dimObj.DimensionText.Length > 0)
  58.                             textValue = dimObj.DimensionText;
  59.                         else
  60.                             textValue = dimObj.FormatMeasurement(dimObj.Measurement, "");
  61.  
  62.                         ed.WriteMessage("\n DimensionText = "
  63.                                         + textValue
  64.                                         + " , Measurement = "
  65.                                         + dimObj.Measurement
  66.                                         );
  67.                         if (kdubUtils.isNumeric(textValue))
  68.                             numValues.Add(Convert.ToDouble(textValue));
  69.                         break;
  70.                 }
  71.             }
  72.             foreach (double val in numValues)
  73.             {
  74.                 numValue += val;
  75.             }
  76.             ed.WriteMessage("\n Total Value = " + numValue);
  77.         }
  78.         catch (Autodesk.AutoCAD.Runtime.Exception ex)
  79.         {
  80.             ed.WriteMessage(ex.Message);
  81.             tr.Abort();
  82.         }
  83.     }
  84. }
  85.  
Quote
>>>
Entity.GetType() : MText
 Text Property = 1338
Entity.GetType() : MText
 Text Property = 457
Entity.GetType() : MText
 Text Property = 761
Entity.GetType() : RotatedDimension
 DimensionText = 1338 , Measurement = 1338.13021112601
Entity.GetType() : RotatedDimension
 DimensionText = PREFIX 624 SUFFIX , Measurement = 624.184381557403
Total Value = 5964




« Last Edit: March 23, 2012, 06:09:40 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.

Glenn R

  • Guest
Re: Programming Fairies needed
« Reply #20 on: April 21, 2007, 01:12:46 AM »
Um... don't you mean:

  if( dimObj is Dimension )

Possibly ;) I had typeof on the brain last night and in the cold light of day, I would probably actually use, in this case, 'as' instead...potentially one less cast.

Code: [Select]
MText mt = dbObj as MText;
If (mt != null) {
  // Got some MText - do your mojo here.
}
DBText t = dbObj as DBText;
if (t != null) {
  // Got some text - do text mojo here
}

Cheers,
Glenn.

Glenn R

  • Guest
Re: Programming Fairies needed
« Reply #21 on: April 21, 2007, 01:15:47 AM »
Kerry, that's as good as anything else - nicely done.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Programming Fairies needed
« Reply #22 on: April 21, 2007, 01:23:12 AM »
Thanks Glenn .. some of the changes you may recognise.

It's been a fun couple of days < in parts >

I've decided that I will make time to hit the API every day ... "Use it or lose it "
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 #23 on: April 21, 2007, 01:41:43 AM »
'As' is good, since 'is' is really just (obj as type) != null;

But I would probably just use

  if( obj is Text )
     ...
  else if( obj is MText )
     ...
  else if( obj is Dimension )
     ...
 
There's two casts for each, and its the price we pay to
have code that can deal any type of object derived from
one of those classes, such as various custom objects in
AutoCAD verticals, or newly introduced objects whose
managed classes derive from those being dealt with.

Let's say for example, in the next release of AutoCAD, or
in a vertical flavor, one or more new type(s) of dimension
objects existed, and their managed type was derived from
Dimension. The code Kerry uses would simply ignore them,
becase he is using explicit comparisons against types.

I came across something similar to this when my code was
used on ADT, and found that it was hitting numerous ADT
custom objects that were derived from Curve, and the user
was amazed at how the code was able to deal with those
objects, as well as the regular AutoCAD Curve derivatives :)


Um... don't you mean:

  if( dimObj is Dimension )

Possibly ;) I had typeof on the brain last night and in the cold light of day, I would probably actually use, in this case, 'as' instead...potentially one less cast.

Code: [Select]
MText mt = dbObj as MText;
If (mt != null) {
  // Got some MText - do your mojo here.
}
DBText t = dbObj as DBText;
if (t != null) {
  // Got some text - do text mojo here
}

Cheers,
Glenn.

Glenn R

  • Guest
Re: Programming Fairies needed
« Reply #24 on: April 21, 2007, 09:00:06 AM »
Hmmmm...agreed Tony.