Thursday, 16 October 2014

Rectangle Centerlines Part 05

...continued from Part 4

Error code 1001

The ValidateRectangle function takes the selection set as an argument and iterates through its members to first check if the user has picked exactly 4 objects.

Private Function ValidateRectangle(ByVal oSelectSet As SelectSet) As Integer
  Dim iErrorCode = 0

  If oSelectSet.Count <> 4 Then
    iErrorCode = 1001
    Return iErrorCode
    Exit Function
  End If

First the iErrorCode is set to 0. If the count of objects in the selection set is not equal to zero i.e the user has either selected either no lines or 1, 2 or 3 lines or more than 4 lines, the error code is set to 1001 and the function returns and Exits immediately without any further validations since none would be necessary if user has not picked exactly 4 objects.

Error code 1002: If there are indeed just 4 objects in the selection set, it checks if they are all lines.

The error code is still 0.

For Each oSide As Object In oSelectSet
  If Not TypeOf oSide Is SolidEdgeFrameworkSupport.Line2d Then
    iErrorCode = 1002
    Exit For
  End If
Next

If iErrorCode <> 0 Then
  Return iErrorCode
  Exit Function
End If

Note the first two lines in code snippet above, they are completely natural language.

Iterating through the For Each loop, the loop Exits immediately when one of the objects in the selection set is not a line. The error code is also set before Exiting.

The two For loops above can perhaps be combined but I haven't tried it yet.

Error code 1003: The next check is geometry intensive in that it validates if the 4 lines are connected. This is done by checking if each of the lines is connected to two other lines from the remaining three.

Lets label the four lines as below:

13

Lets call the first line as this line. To check if its start and end points are connected to any of the start or end points of other lines, lets identify the lines as below:

14

The variables for the this line are as below:

Dim startXthis As Double = 0, startYthis As Double = 0
Dim endXthis As Double = 0, endYthis As Double = 0

Dim startXother As Double = 0, startYotherAs Double = 0
Dim endXother As Double = 0, endYother As Double = 0

Also declare a list to store all lines in the selection set called oListOfAllLines and another one to store just the other lines:

Dim oListOfAllLines As List(Of Line2d) = Nothing
Dim oListOfOtherLines As List(Of Line2d) = Nothing

Also declare Boolean variables for storing the status if the Start and End Point match and finally to store if the Line is connected at both ends:

Dim StartPointMatch As Boolean
Dim EndPointMatch As Boolean
Dim LineIsConnected As Boolean = True

Bring the list of all lines to life:

oListOfAllLines = New List(Of Line2d)

and flood it with all lines in the selection set:

For Each oLine As Line2d In oSelectSet
  oListOfAllLines.Add(oLine)
Next

Next loop through all the lines and inside the loop, flood the list of other lines too with all lines but immediately remove the this line from the list so you have a list of just the other lines:

For i As Integer = 0 To oListOfAllLines.Count - 1
  oListOfOtherLines = New List(Of Line2d)

  For Each oLine As Line2d In oListOfAllLines
    oListOfOtherLines.Add(oLine)
  Next
  oListOfOtherLines.RemoveAt(i)

Get the start and end point coordinates of the this line:

oListOfAllLines.Item(i).GetStartPoint(startXthis, startYthis)
oListOfAllLines.Item(i).GetEndPoint(endXthis, endYthis)

Continued to Part 6...

Index of all Solid Edge Tutorials, Tips, Videos...