Author Topic: Point3d X value  (Read 2558 times)

0 Members and 1 Guest are viewing this topic.

shers

  • Guest
Point3d X value
« on: May 18, 2015, 12:07:03 PM »
Hi,

It is not possible to assign value to Point3d Coordinates one by one, like Point3d insPt.X = 2. Is there any other option?

Thanks

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: Point3d X value
« Reply #1 on: May 18, 2015, 12:37:35 PM »
Point3d newpt = new Point3d(2, insPt.Y, insPt.Z);

BlackBox

  • King Gator
  • Posts: 3770
Re: Point3d X value
« Reply #2 on: May 18, 2015, 12:43:26 PM »
"How we think determines what we do, and what we do determines what we get."

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Point3d X value
« Reply #3 on: May 18, 2015, 12:45:06 PM »
Hi,

The type Point3d, as many others .NET structures, is immutable.
You cannot edit a Point3d, you have to create a new one.
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Point3d X value
« Reply #4 on: May 18, 2015, 04:40:38 PM »
If you really need a mutable point to improve performances* while you make many changes to a point coordinates, you can build a little class where the point coordinates are read/write enabled.
The mutable point type may have explicit cast operators from and to the Point3d type.
Code - C#: [Select]
  1. using Autodesk.AutoCAD.Geometry;
  2.  
  3. namespace MutablePoint
  4. {
  5.     public class Point
  6.     {
  7.         public Point(double x, double y, double z)
  8.         {
  9.             this.X = x;
  10.             this.Y = y;
  11.             this.Z = z;
  12.         }
  13.  
  14.         public double X { get; set; }
  15.         public double Y { get; set; }
  16.         public double Z { get; set; }
  17.  
  18.         public void Add(Vector3d disp)
  19.         {
  20.             this.X += disp.X;
  21.             this.Y += disp.Y;
  22.             this.Z += disp.Z;
  23.         }
  24.  
  25.         public static explicit operator Point(Point3d pt)
  26.         {
  27.             return new Point(pt.X, pt.Y, pt.Z);
  28.         }
  29.  
  30.         public static explicit operator Point3d(Point pt)
  31.         {
  32.             return new Point3d(pt.X, pt.Y, pt.Z);
  33.         }
  34.     }
  35. }

Using:
Code - C#: [Select]
  1. Point pt = (Point)Point3d.Origin;
  2. pt.X = 10.0;
  3. pt.Y = 20.0;
  4. Point3d p3d = (Point3d)pt; // p3d = (10,20,0)

*mutable objects are more efficient with memory but immutable ones are safer ans easier to use in concurent programming.
Speaking English as a French Frog

shers

  • Guest
Re: Point3d X value
« Reply #5 on: May 19, 2015, 01:56:32 AM »
That's great! Thanks very much!

I also would like to know how to assign null value to Point3d, like
Point3d pt = null;

Thanks
« Last Edit: May 19, 2015, 05:16:56 AM by sresh »

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Point3d X value
« Reply #6 on: May 19, 2015, 07:16:09 AM »
Hi,

It looks like you're missing very basics .NET.
As I said upper Point3d is a structure, not a class. Structures are value types and classes are reference type and a structure instance must have a value and cannot be null (see here).
So, you can use a Nullable type (Nullable<Point3d> or Point3d?) or use the Point class (as defined upper) instead of Point3d structure.

Anyway, I'm very curious about what kind of stuff you're doing which requires mutable and nullable points...
Speaking English as a French Frog

ChrisCarlson

  • Guest
Re: Point3d X value
« Reply #7 on: May 19, 2015, 08:56:59 AM »
:confused: how can a coordinate system contain a null value either in the X,Y,Z or the entire value?