2016-10-28 3 views
0

での描きながら、別のウィンドウに渡し、キャンバスの上に延長し、ポリラインなどキャンバスに同じジオメトリを持つオブジェクトを作成します。 AutoCADオブジェクトはポリラインなので、いくつかの点(頂点)をオーバードライする必要があります。ポリラインは、私は既存のジオメトリからポイントを得るAutoCADのプラグインを作成していPointCollection C#WPFの使用

Points in autocad

Iは、頂点の長さと、AutoCADからのポイントを収集し、キャンバス上の実際の座標で中心オブジェクトにそれを変換します。私はそれを描くときに、私はこれを取得:私は収集

enter image description here

のポイントは、彼らがsimetrically変換され、正しいです。そして、キャンバスの上に描画するためのコードをここにある:

private void drawOnCanvas(List<Point> points) 
    { 
     // Create a black Brush 
     SolidColorBrush blackBrush = new SolidColorBrush(); 
     blackBrush.Color = Colors.Black; 

     // Create a polyline 
     Polyline poly = new Polyline(); 
     poly.Stroke = blackBrush; 
     poly.StrokeThickness = 4; 

     // Create a collection of points for a polyline 
     PointCollection polygonPoints = new PointCollection(); 
     for (int i = 0; i < points.Count-1; i++) 
     { 
      polygonPoints.Add(points[i]); 
     } 

     // Set Polyline.Points properties 
     poly.Points = polygonPoints; 

     // Add polyline to the page 
     canvas.Children.Add(poly); 
    } 

私はデバッガを入力すると、ポイントは次のように表示されます。ご覧のとおり

enter image description here

は、ポイントは正しい方法で定義されています。

private void canvas_MouseMove(object sender, MouseEventArgs e) 
    { 
     Point p = Mouse.GetPosition(canvas); 
     coords.Content = p.ToString(); 
    } 

私はmouseMoveで座標を読み取ると、長辺(50/2)の約半分の長辺が長くなります。

これはなぜ起こるのでしょおよびこの問題を解決する方法?

更新ソリューション:

for (int i = 0; i < points.Count - 1; i += 2) 
       { 
        pathGeometry = pathGeometry + "M" + points[i].X.ToString("F2") + " " + points[i].Y.ToString("F2") + " " + "L" + points[i + 1].X.ToString("F2") + " " + points[i + 1].Y.ToString("F2") + " "; 
       } 

       canvas.Children.Add(new Path { Stroke = Brushes.Brown, StrokeThickness = 3, Data = Geometry.Parse(pathGeometry) }); 

更新ソリューション2:(beter液)

PathFigure figures = new PathFigure(); 
figures.StartPoint = points[0]; 
points.RemoveAt(0); 
figures.Segments = new PathSegmentCollection(points.Select((p, i) => new LineSegment(p, i % 2 == 0))); 
PathGeometry pg = new PathGeometry(); 
pg.Figures.Add(figures); 
canvas.Children.Add(new Path { Stroke = Brushes.Brown, StrokeThickness = 3, Data = pg }); 
+0

2つのノートの弧のようsegmensの他のタイプを使用することができますので、表す形状の種類を知る必要がありますどちらの場合も

。新しいSolidColorBrushを作成する代わりに 'poly.Stroke = Brushes.Black;'と書くことができます。 PointCollectionは 'IEnumerable '引数をとるコンストラクタを持っているので、 'for'文全体を削除して' poly.Points = new PointCollection(points); 'と書くだけです。全体のメソッドは、 'canvas.Children.Add(新しいポリライン{ストローク=ブラシ、ブラシ、StrokeThickness = 4、ポイント=新しいPointCollection(ポイント)});と書くことができます – Clemens

+0

それを指摘してくれてありがとう、 (int i = 0; i dodoria1992

+1

'新しいPointCollection(points.Take(points.Count - 1))'を書くことができます。 'Take'は' System.Linq.Enumerable'の拡張メソッドです。 – Clemens

答えて

0

私はあなたがトップの中間点に、右上の点から電源を入れたときからだと思いますポリゴンの描画は実際のポイントを超えて一種の鋭い頂点を作るように拡張されます。しかし、私はあなたがy軸を下向きにしてウィンドウを描くと思うので、私の右上を底辺のように考える必要があります:-)それは右の短い辺を意味します。

<Path Data="M10 5 L60 5 M35 5 L35 65 M10 65 L60 65" Stroke="Black" StrokeThickness="5" /> 

を更新:

溶液は異なるラインまたはPathGeometryを使用することで、上記第一の図面を参照して

を、それが実際にこの順序で描かれています:5-6- 4-3-1-2。 (y軸が下にあるため)。

あなたがキャンバスに以下の2つの形状を挿入する場合は、ポリラインのレンダリングとパスの違いが表示されます。ポリラインで値20を別の値に変更すると、ポリラインの(デザインごとの)レンダリングの効果がパスに比べてわかります。ポリラインとパスの両方のポイントは任意ですが、代わりにACadポイントを使用する必要があります。

<Polyline Name="Poly" Points="10 5, 60 5, 35, 20 35, 65, 10, 65 60, 65" Stroke="Blue" StrokeThickness="5"> 
</Polyline> 

<Path Name="MyPath" Data="M10 5 L60 5 M35 5 L35 65 M10 65 L60 65" Stroke="Black" StrokeThickness="5"> 
    <Path.RenderTransform> 
    <TranslateTransform X="0" Y="100" /> 
    </Path.RenderTransform> 
</Path> 

詳細については、hereをご覧ください。

アップデート2:

コードでは、文字列としてパスのデータを構築することは非常に厄介なことができるの後ろに。

PathFigure figures = new PathFigure(new Point(10, 5), 
    new PathSegment[] 
    { 
     new LineSegment(new Point(60,5), true), 
     new LineSegment(new Point(35,5), false), 
     new LineSegment(new Point(35,65), true), 
     new LineSegment(new Point(10,65), false), 
     new LineSegment(new Point(60,65), true), 
    }, false); 

    PathGeometry pg = new PathGeometry(); 
    pg.Figures.Add(figures); 
    MyPath.Data = pg; 

それとも一連の点から行うことができます:代わりに、対応するタイプを使用することをお勧めします上記の

Point[] points = 
    { 
    new Point(60, 5), 
    new Point(35, 5), 
    new Point(35, 65), 
    new Point(10, 65), 
    new Point(60, 65), 
    }; 

    PathFigure figures = new PathFigure(); 
    figures.StartPoint = new Point(10, 5); 
    figures.Segments = new PathSegmentCollection(points.Select((p, i) => new LineSegment(p, i % 2 == 0))); 
    PathGeometry pg = new PathGeometry(); 
    pg.Figures.Add(figures); 
    MyPath.Data = pg; 

をMYPATHのデータ列と同じように行います。あなたはポイントは、あなたがそれに応じisStrokedを設定、または多分など

+0

私は実際にここにポイントを得ていません。あなたはパスデータについてもっと詳しく説明できますか? – dodoria1992

+0

@ dodoria1992:上記の更新を参照してください。 –

+0

ありがとう.. ..今私は違いを気づいた...ポイントをパスポイントに変換する方法を理解する必要があります。 – dodoria1992

関連する問題