Wednesday, 15 October 2014

Rectangle Centerlines Part 04

...continued from Part 3

This is followed by checking if a document is open:

oDoc = oApp.ActiveDocument

If oDoc Is Nothing Then
  MessageBox.Show("A document should be open.", sTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
  End
End If

If (oDoc.Type <> SolidEdgeFramework.DocumentTypeConstants.igPartDocument) Then
  If (oDoc.Type <> SolidEdgeFramework.DocumentTypeConstants.igDraftDocument) Then
    MessageBox.Show("This program works in a Part or Draft document only.", sTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
  End
  End If
End If

If there indeed is a document open, check if it is a Part or Draft type and accordingly, set the Units of Measure variables and additionally in case of Draft document, the layers list box is flooded.

If it is a Part document, then also check if the Sketcher mode is active using the ActiveEnvironment property:

If oDoc.Type = SolidEdgeFramework.DocumentTypeConstants.igPartDocument Then
  oDocP = oApp.ActiveDocument
  If oApp.ActiveEnvironment <> "LayoutInPart" Then
    MessageBox.Show("Enter sketch mode in the Part document.", sTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End
  Else

    oUoM = oDocP.UnitsOfMeasure
  End If
ElseIf
oDoc.Type = SolidEdgeFramework.DocumentTypeConstants.igDraftDocument Then
  oDocD = oApp.ActiveDocument
  oUoM = oDocD.UnitsOfMeasure

  oSheet = oDocD.ActiveSheet
  Dim oLayers As Layers = oSheet.Layers
  For Each oLayer As Layer In oLayers
    lstLayers.Items.Add(oLayer.Name)
  Next
  lstLayers.SelectedIndex = 0
End If

For adding all existing layer names to the Layers listbox, a For Each loop is used which iterates through each layer of the current sheet's Layers collection and adds the name of each layer to the listbox. It also selects the first i.e. 0 th item in the list.

Finally in the Form's Load event, a Select Case statement is used to check an error code returned by the ValidateRectangle function.

The ValidateRectangle function returns an integer which is an error code with values like 1002,1002, 1003, 1004 and so on.. each of which indicates an error condition regarding the selection set passed to it.

The selection set consists of the objects picked by the user which are assumed to be the lines that form the rectangle.

The program must thoroughly check the objects picked for validity before actually drawing the centerlines.

oSelectSet = oDoc.SelectSet
Dim iErrorCode As Integer = ValidateRectangle(oSelectSet)

Select Case iErrorCode
  Case 1001
    MessageBox.Show("Select 4 lines that form a rectangle.", sTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End

  Case 1002
    MessageBox.Show("Not all selected objects are lines.", sTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End

  Case 1003
    MessageBox.Show("Selected lines are disconnected.", sTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End

  Case 1004
    MessageBox.Show("Selected lines are not continuous i.e. no drawn End-To-End.", sTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End
End Select

lets look each of the geometry intensive validations in the next few parts.

Continued to Part 5...

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