Author Topic: Breaking is Broken  (Read 2285 times)

0 Members and 1 Guest are viewing this topic.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Breaking is Broken
« on: November 20, 2016, 12:47:26 AM »
I've messed with this a ton but I can't see why it's failing to break my arcs at the point locations specified.  Can anyone see what I'm missing?

Code - C#: [Select]
  1.           DBObjectCollection newCurves = new DBObjectCollection();
  2.                         foreach (ObjectId myid in originalcollection)
  3.                         {
  4.                             if (myid.GetType() == typeof(Arc))
  5.                             {
  6.                                 Curve curve = myid.GetObject(OpenMode.ForRead) as Curve;
  7.                                 List<double> lst = new List<double>();
  8.                                 DoubleCollection paramcollection = new DoubleCollection();
  9.                                 foreach (Point3d pnt in pntcoll)
  10.                                     if (curve.GetClosestPointTo(pnt,false).DistanceTo(pnt) < 0.1)
  11.                                     lst.Add(curve.GetParameterAtPoint(pnt));
  12.                                 lst.Sort();
  13.                                 foreach (double param in lst)
  14.                                     paramcollection.Add(param);
  15.                                 newCurves = curve.GetSplitCurves(paramcollection);
  16.                                 for (int i = 0; i < newCurves.Count; i++)
  17.                                 {
  18.                                     Entity pent = (Entity)newCurves[i] as Entity;
  19.                                     pent.ColorIndex = i + 1;// to display result only
  20.                                     btr.AppendEntity(pent);
  21.                                     MyTrans.AddNewlyCreatedDBObject(pent, true);
  22.                                     MyTrans.TransactionManager.QueueForGraphicsFlush();
  23.                                 }
  24.                             }
  25.                         }


gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Breaking is Broken
« Reply #1 on: November 20, 2016, 05:53:45 AM »
Hi,

I can't see what's wrong with your code, but why do you use curve parameters instead of directly using the points ?
Curve paramaters are not related to the same thing according to the curve type. With arcs, the parameters represents angles.

Code - C#: [Select]
  1. var points = new Point3dCollection(pntcoll)
  2.     .Cast<Point3d>()
  3.     .Where(p => curve.GetClosestPointTo(p, false).DistanceTo(p) < 0.1)
  4.     .Select(p => curve.GetClosestPointTo(p, false))
  5.     .OrderBy(p => curve.GetDistAtPoint(p))
  6.     .ToArray());
  7. newCurves = curve.GetSplitCurves(points);
Speaking English as a French Frog

n.yuan

  • Bull Frog
  • Posts: 348
Re: Breaking is Broken
« Reply #2 on: November 20, 2016, 08:52:20 AM »
I've messed with this a ton but I can't see why it's failing to break my arcs at the point locations specified.  Can anyone see what I'm missing?

Code - C#: [Select]
  1.           DBObjectCollection newCurves = new DBObjectCollection();
  2.                         foreach (ObjectId myid in originalcollection)
  3.                         {
  4.                             if (myid.GetType() == typeof(Arc))
  5.                             {
  6.                                 ...
  7.                                 }
  8.                             }
  9.                         }


If you looked at the "if...." statement in the "for..." loop carefully, or if you ran the  code with BREAK POINT inside the loop or inside the "if..." block, you would have known why the code did not work: all the code enclosed by "if..." is bypassed, because an ObjectID cannot be a type of "Arc".

You should use

if (myid.ObjectClass.DxfName.ToUpper()=="ARC")...

or

var arc=myid.GetObject(....) as Arc;
if (arc!=null)
{
    ....
}

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Breaking is Broken
« Reply #3 on: November 20, 2016, 09:06:11 AM »
Well seen, Norman.
Speaking English as a French Frog

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Breaking is Broken
« Reply #4 on: November 20, 2016, 10:49:06 AM »
Thank you both.  It explains why it worked on my line segments but not arcs. I had the object cast to Line entity first. I need to learn to use the break points / debugger.

Much appreciated.
« Last Edit: November 20, 2016, 10:54:49 AM by Area51Visitor »

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Breaking is Broken
« Reply #5 on: November 20, 2016, 10:50:53 AM »
Hi,

I can't see what's wrong with your code, but why do you use curve parameters instead of directly using the points ?
Curve paramaters are not related to the same thing according to the curve type. With arcs, the parameters represents angles.

Code - C#: [Select]
  1. var points = new Point3dCollection(pntcoll)
  2.     .Cast<Point3d>()
  3.     .Where(p => curve.GetClosestPointTo(p, false).DistanceTo(p) < 0.1)
  4.     .Select(p => curve.GetClosestPointTo(p, false))
  5.     .OrderBy(p => curve.GetDistAtPoint(p))
  6.     .ToArray());
  7. newCurves = curve.GetSplitCurves(points);
Thanks for the better approach giles, much appreciated.
« Last Edit: November 20, 2016, 10:55:42 AM by Area51Visitor »

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Breaking is Broken
« Reply #6 on: November 20, 2016, 01:15:56 PM »
Had a few more things wrong with it...it's sloppy but is working. This is what I ended up with.

I'll be looking to tweak it per Giles comments though.

Code - C#: [Select]
  1.  //try to split curves
  2.                         foreach (ObjectId objid in originalcollection)
  3.                         {
  4.                             try
  5.                             {
  6.                                 Curve curve = objid.GetObject(OpenMode.ForRead) as Curve;
  7.  
  8.                                 Point3dCollection breakpoints = new Point3dCollection();
  9.                                 foreach (BlockReference mybr in myblocks)
  10.                                 {
  11.                                     if ((curve.GetClosestPointTo(mybr.Position, false).DistanceTo(mybr.Position) < 0.1) & (curve.StartPoint.DistanceTo(mybr.Position) > 0.1) & (curve.EndPoint.DistanceTo(mybr.Position) > 0.1))
  12.                                         breakpoints.Add(mybr.Position);
  13.                                 }
  14.  
  15.                                 List<double> paramatersforpoints = new List<double>();
  16.                                 foreach (Point3d bkpnt in breakpoints)
  17.                                 {
  18.                                     paramatersforpoints.Add(curve.GetParameterAtPoint(bkpnt));
  19.                                 }
  20.                                 paramatersforpoints.Sort();
  21.  
  22.                                 Point3dCollection sortedpoints = new Point3dCollection();
  23.                                 foreach (double param in paramatersforpoints)
  24.                                 {
  25.                                     sortedpoints.Add(curve.GetPointAtParameter(param));
  26.                                 }
  27.  
  28.                                 DBObjectCollection newCurves = curve.GetSplitCurves(sortedpoints);
  29.                                 if (newCurves.Count != 0)
  30.                                 {
  31.                                     for (int i = 0; i < newCurves.Count; i++)
  32.                                     {
  33.                                         Entity pent = (Entity)newCurves[i] as Entity;
  34.                                         pent.ColorIndex = i + 1;// to display result only
  35.                                         btr.AppendEntity(pent);
  36.                                         MyTrans.AddNewlyCreatedDBObject(pent, true);
  37.                                         MyTrans.TransactionManager.QueueForGraphicsFlush();
  38.                                         originalcollection.Add(pent.ObjectId);
  39.                                         originalcollection.Remove(curve.ObjectId);
  40.                                     }
  41.                                 }
  42.                             }
  43.                             catch
  44.                             {
  45.                             }
  46.                         }                                          
  47.                         //end splits
  48.  
« Last Edit: November 20, 2016, 01:50:31 PM by Area51Visitor »