问题描述,有一系列点,想构成面,而面由ring构成,ring由Segment构成,所以先由点构成Segment。由于点变量比较多,不想再定义相同多的 tempLine变量,仅仅是把它清空了又在构成下一个线段时利用,结果出现之前的pSegment变量的值被改变:
ILine tempLine = new LineClass();
IPoint p1 = new PointClass();
IPoint p2 = new PointClass();
p1.X = 502225.693493928;
p1.Y = 401985.666916852;
p2.X = 502185.696188842;
p2.Y = 401985.202822681;
tempLine.PutCoords(p1, p2);
ISegment pSegment1 = tempLine as ISegment;//注意这里
Console.WriteLine(pSegment1.FromPoint.X+ " "+pSegment1.FromPoint.Y+ " "+ pSegment1.ToPoint.X+" "+ pSegment1.ToPoint.Y);
//如果用SetEmpty()就会导致pSegment的两端点跟着改变
tempLine.SetEmpty();
IPoint pp1 = new PointClass();
IPoint pp2 = new PointClass();
pp1.X = 602225.693493928;
pp1.Y = 701985.666916852;
pp2.X = 602185.696188842;
pp2.Y = 701985.202822681;
tempLine.PutCoords(pp1, pp2);
ISegment pSegment2 = tempLine as ISegment;//注意这里
Console.WriteLine(pSegment.FromPoint.X + " " + pSegment.FromPoint.Y + " " + pSegment.ToPoint.X + " " + pSegment.ToPoint.Y);//与上边打印的结果不一样
解决的办法: 为新的Segment定义新的tempLine变量。