2016-10-28 46 views
0

我正在創建autocad插件,它從現有的幾何圖形獲取點,將其傳遞到另一個窗口並使用折線在畫布上創建具有相同幾何圖形的對象。 Autocad對象是多段線,所以一些點(頂點)必須透支。在使用PointCollection透視時在畫布上擴展的多義線WP#

Points in autocad

我收集點從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:(尤爲明顯的解決方案)

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

有兩點需要注意兩種情況。你可以寫'poly.Stroke = Brushes.Black;'而不是創建一個新的SolidColorBrush。 PointCollection有一個構造函數,它接受一個'IEnumerable '參數,所以你可以刪除整個'for'語句並且只寫'poly.Points = new PointCollection(points);'。整個方法可以寫成:canvas.Children.Add(new Polyline {Stroke = Brushes.Black,StrokeThickness = 4,Points = new PointCollection(points)});' – Clemens

+0

謝謝你指出,但我可以'使用'Points = new PointCollection(points)',因爲我少了1分..在我的代碼中看到'for(int i = 0; i dodoria1992

+1

你可以寫'新的PointCollection(points.Take(points.Count - 1))'。 'Take'是'System.Linq.Enumerable'中的擴展方法。 – Clemens

回答

0

我想這是因爲當你從右上角點轉向頂部中間點多邊形的繪製延伸到了製造一種尖銳頂點的實際點上。但我認爲你用Y軸向下畫窗口,所以你不得不把我的右上角看作是底部的斜線:-)這意味着它是短邊是正確的。

的解決方案是使用不同的線或的PathGeometry:

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

更新:

參照上面的第一繪製,它實際上是在繪製該序列:5-6- 4-3-1-2。 (因爲y軸向下)。

如果將以下兩個圖形插入到Canvas中,則會看到折線渲染和路徑之間的區別。如果將Polyline中的值20更改爲另一個值,則會看到Polyline(每個設計)渲染的效果 - 與Path相比。我在折線和路徑中的點都是任意的,你應該使用你的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> 

如果你想了解更多關於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,或許使用其他類型的segmens像圓弧等

+0

我真的不明白這裏的意思..你能解釋更多關於路徑數據的細節嗎? – dodoria1992

+0

@ dodoria1992:看到我上面的更新。 –

+0

謝謝..現在我注意到了區別...只需要弄清楚如何將點轉換爲路徑點.. – dodoria1992