combining two codes

Forums: 

hello there,
i have two codes, which are based on SNAP. The 1st one extracts the data from the line & curve. The second one asks user to enter data , shown by 1st code. The second code has some formulas, which generates circle. I want to combine these two codes i.e I want second code to directly take values given by 1st code. sending both the codes here with, please help

1st code

Option Infer On
Imports Snap, Snap.Create

' This is a very simple example showing you how to use a Snap.UI.BlockForm
' to create a dialog. You may add and remove blocks to suit your needs.
' Please see SNAP Getting Started Guide for an introduction
' to the related concepts.

Public Class SnapBlockFormApplication61 : Inherits UI.BlockForm

' Declarations of the blocks on a SnapBlockFormApplication61 dialog
Dim Linex As UI.Block.SelectObject
Dim arc1 As UI.Block.SelectObject
Dim Liney As UI.Block.SelectObject
Dim arc2 As UI.Block.SelectObject
Dim Linez As UI.Block.SelectObject
' Constructor for a SnapBlockFormApplication61 dialog object
Public Sub New()

Me.Title = "SnapBlockFormApplication61" ' Text to be shown in title bar of dialog
Me.Cue = "Please enter information" ' Text to be shown in cue line

' Create an Enumeration block (for example)
Linex = New UI.Block.SelectObject
Linex.LabelString = "Linex"
Linex.SetFilter(Snap.NX.ObjectTypes.Type.Line)
Linex.MaximumScope = UI.Block.SelectionScope.AnyInAssembly

arc1 = New UI.Block.SelectObject
arc1.LabelString = "Arc1"
arc1.SetFilter(Snap.NX.ObjectTypes.Type.Arc)
arc1.MaximumScope = UI.Block.SelectionScope.AnyInAssembly

' Create an Integer block (for example)
Liney = New UI.Block.SelectObject
Liney.LabelString = "Liney"
Liney.SetFilter(Snap.NX.ObjectTypes.Type.Line)
Liney.MaximumScope = UI.Block.SelectionScope.AnyInAssembly
arc2 = New UI.Block.SelectObject
arc2.LabelString = "Arc2"
arc2.SetFilter(Snap.NX.ObjectTypes.Type.Arc)
arc2.MaximumScope = UI.Block.SelectionScope.AnyInAssembly

' Create a Button block (for example)
Linez = New UI.Block.SelectObject
Linez.LabelString = "Linez"
Linez.SetFilter(Snap.NX.ObjectTypes.Type.Line)
Linez.MaximumScope = UI.Block.SelectionScope.AnyInAssembly

' Create addition blocks here, as you wish, and delete
' any of the ones above, if you don’t need them.

' Add all the blocks to the BlockForm
Me.AddBlocks(Linex, arc1, Liney, arc2, Linez)

End Sub

Public Shared Sub Main()

' Create and display a SnapBlockFormApplication61 dialog
Dim myForm = New SnapBlockFormApplication61()
myForm.Show()

End Sub

Public Overrides Sub OnShow()
' Code for when dialog is displayed
End Sub

Public Overrides Sub OnOK()
' Code for when user clicks OK
End Sub

Public Overrides Sub OnApply()
Dim x As NX.Line = Linex.SelectedObjects(0)
Dim y As NX.Line = Liney.SelectedObjects(0)
Dim z As NX.Line = Linez.SelectedObjects(0)
Dim a1 As NX.Arc = arc1.SelectedObjects(0)
Dim a2 As NX.Arc = arc2.SelectedObjects(0)
Dim p1 As Position = x.StartPoint
Dim PX1 As Double = x.StartPoint.X
Dim L1 As Integer = x.ArcLength
InfoWindow.WriteLine("L1=" & L1.ToString)
InfoWindow.WriteLine("P1=" & p1.ToString)
InfoWindow.WriteLine("px1=" & PX1.ToString)
Dim L2 As Integer = a1.ArcLength
Dim R2 As Double = a1.Radius
InfoWindow.WriteLine("L2=" & L2.ToString)
InfoWindow.WriteLine("R2=" & R2.ToString)
Dim P3 As Position = y.StartPoint
Dim PX3 As Double = y.StartPoint.X
Dim L3 As Integer = y.ArcLength
InfoWindow.WriteLine("L3=" & L3.ToString)
InfoWindow.WriteLine("P3=" & P3.ToString)
InfoWindow.WriteLine("PX3=" & PX3.ToString)
Dim L4 As Integer = a2.ArcLength
Dim R4 As Double = a2.Radius
InfoWindow.WriteLine("L4=" & L4.ToString)
InfoWindow.WriteLine("r1=" & R4.ToString)

Dim P5 As Position = z.StartPoint
Dim PX5 As Double = z.StartPoint.X
Dim L5 As Integer = z.ArcLength
InfoWindow.WriteLine("L3=" & L5.ToString)
InfoWindow.WriteLine("P3=" & P5.ToString)
InfoWindow.WriteLine("p31=" & PX5.ToString)

End Sub

Public Overrides Sub OnCancel()
' Code for when user clicks Cancel
End Sub

Public Overrides Sub OnUpdate(ByVal changedBlock As UI.Block.General)

End Sub

End Class

2nd code

Option Infer On
Imports Snap, Snap.Create, Snap.Math
Public Class SnapBlockFormApplication16 : Inherits UI.BlockForm

' Declarations of the blocks on a SnapBlockFormApplication16 dialog
Dim L1 As UI.Block.Integer
Dim PX1 As UI.Block.Integer
Dim L2 As UI.Block.Integer
Dim R2 As UI.Block.Integer
Dim L3 As UI.Block.Integer
Dim PX3 As UI.Block.Integer
Dim L4 As UI.Block.Integer
Dim R4 As UI.Block.Integer
Dim L5 As UI.Block.Integer
Dim PX5 As UI.Block.Integer
' Constructor for a SnapBlockFormApplication16 dialog object
Public Sub New()

Me.Title = "SnapBlockFormApplication16" ' Text to be shown in title bar of dialog
Me.Cue = "Please enter information" ' Text to be shown in cue line

L1 = New UI.Block.Integer
L1.Label = "L1"
PX1 = New UI.Block.Integer
PX1.Label = "PX1"
L2 = New UI.Block.Integer
L2.Label = "L2"
R2 = New UI.Block.Integer
R2.Label = "R2"
L3 = New UI.Block.Integer
L3.Label = "L3"
L4 = New UI.Block.Integer
L4.Label = "L4"
R4 = New UI.Block.Integer
R4.Label = "R4"
L5 = New UI.Block.Integer
L5.Label = "L5"
PX5 = New UI.Block.Integer
PX5.Label = "PX5"

Me.AddBlocks(L1, PX1, L2, R2, L3, PX3, L4, R4, L5, PX5)

End Sub

Public Shared Sub Main()

Dim myForm = New SnapBlockFormApplication16()
myForm.Show()

End Sub

Public Overrides Sub OnShow()

End Sub

Public Overrides Sub OnOK()

End Sub

Public Overrides Sub OnApply()
Dim a As Integer = L1.Value
Dim a1 As Integer = PX1.Value
Dim X1 As Integer
X1 = a1 + 0.5 * a
InfoWindow.WriteLine("X1=" & X1.ToString)
Dim d As Integer = L2.Value
Dim d1 As Integer = R2.Value
Dim X2 As Double
X2 = (a1 - (a1 / 180))
InfoWindow.WriteLine("X2=" & X2.ToString)
Dim b As Integer = L3.Value
Dim b1 As Integer = PX3.Value
Dim X3 As Integer
X3 = b1
InfoWindow.WriteLine("X3=" & X3.ToString)
Dim e As Integer = L4.Value
Dim e1 As Integer = R4.Value
Dim X4 As Double
X4 = (b1 - (b1 / 180))
InfoWindow.WriteLine("X4=" & X4.ToString)
Dim c As Integer = L5.Value
Dim c1 As Integer = PX5.Value
Dim X5 As Integer
X5 = c1 + 0.5 * c
InfoWindow.WriteLine("X3=" & X3.ToString)
Dim Xt As Double
Xt = ((a * X1) + (b * X3) + (c * X5) + (d * X2) + (e * X4)) / (a + b + c + d + e)
InfoWindow.WriteLine("xt=" & Xt.ToString)
Dim a11 As Double
a11 = 2 * System.Math.PI * (a + b + c + d + e)
Dim d11 As Double
d11 = 2 * System.Math.Sqrt(a11 / System.Math.PI)
InfoWindow.WriteLine("diameter=" & d11.ToString)
Dim d22 As Double = 2 * d11
Dim section As NX.Arc = Circle(0, 0, d22)
Dim axis As Vector = Vector.AxisZ
Dim length As Double = 1
Dim draft As Double = 0
Dim f As NX.Extrude
f = Extrude({section}, axis, length, draft)
End Sub

Public Overrides Sub OnCancel()

End Sub

Public Overrides Sub OnUpdate(ByVal changedBlock As UI.Block.General)

End Sub

End Class

The combined code is below. It looks like the lines and arcs that you select have to obey some rules. I don't know what those rules are, so I can't test the code.

The code that defines the line and arc selection blocks is repetitive, so I factored it out.

There were many places where you had "Integer" declarations that should have been "Double".

The math is weird. Look at the code that defines a11 and d11, for example.

Please test this version. If it works the way you want, I can then shorten and clarify it considerably.

Option Infer On
Imports Snap, Snap.Create, Snap.Math

Public Class MyProgram : Inherits UI.BlockForm

' Declarations of the blocks on a MyProgram dialog
Dim Linex As UI.Block.SelectObject
Dim Liney As UI.Block.SelectObject
Dim Linez As UI.Block.SelectObject

Dim arc1 As UI.Block.SelectObject
Dim arc2 As UI.Block.SelectObject

' Constructor for a MyProgram dialog object
Public Sub New()

Me.Title = "Select Curves" ' Text to be shown in title bar of dialog
Me.Cue = "Please select lines and arcs" ' Text to be shown in cue line

' Create three blocks to select lines
Linex = CreateSelectLineBlock("Linex")
Liney = CreateSelectLineBlock("Liney")
Linez = CreateSelectLineBlock("Linez")

' Create two blocks to select arcs
arc1 = CreateSelectArcBlock("Arc1")
arc2 = CreateSelectArcBlock("Arc2")

' Add all the blocks to the BlockForm
Me.AddBlocks(Linex, arc1, Liney, arc2, Linez)

End Sub

Public Shared Sub Main()

' Create and display a MyProgram dialog
Dim myForm = New MyProgram()
myForm.Show()

End Sub

Public Overrides Sub OnApply()
Dim x As NX.Line = Linex.SelectedObjects(0)
Dim y As NX.Line = Liney.SelectedObjects(0)
Dim z As NX.Line = Linez.SelectedObjects(0)

Dim ar1 As NX.Arc = arc1.SelectedObjects(0)
Dim ar2 As NX.Arc = arc2.SelectedObjects(0)

Dim p1 As Position = x.StartPoint
Dim PX1 As Double = x.StartPoint.X
Dim L1 As Double = x.ArcLength
InfoWindow.WriteLine("L1=" & L1.ToString)
InfoWindow.WriteLine("P1=" & p1.ToString)
InfoWindow.WriteLine("px1=" & PX1.ToString)

Dim L2 As Double = ar1.ArcLength
Dim R2 As Double = ar1.Radius
InfoWindow.WriteLine("L2=" & L2.ToString)
InfoWindow.WriteLine("R2=" & R2.ToString)

Dim P3 As Position = y.StartPoint
Dim PX3 As Double = y.StartPoint.X
Dim L3 As Double = y.ArcLength
InfoWindow.WriteLine("L3=" & L3.ToString)
InfoWindow.WriteLine("P3=" & P3.ToString)
InfoWindow.WriteLine("PX3=" & PX3.ToString)

Dim L4 As Double = ar2.ArcLength
Dim R4 As Double = ar2.Radius
InfoWindow.WriteLine("L4=" & L4.ToString)
InfoWindow.WriteLine("r1=" & R4.ToString)

Dim P5 As Position = z.StartPoint
Dim PX5 As Double = z.StartPoint.X
Dim L5 As Double = z.ArcLength
InfoWindow.WriteLine("L3=" & L5.ToString)
InfoWindow.WriteLine("P3=" & P5.ToString)
InfoWindow.WriteLine("p31=" & PX5.ToString)

' Do the geometry construction

Dim a As Double = L1
Dim a1 As Double = PX1
Dim X1 As Double = a1 + 0.5 * a
InfoWindow.WriteLine("X1=" & X1.ToString)

Dim d As Double = L2
Dim d1 As Double = R2
Dim X2 As Double = (a1 - (a1 / 180))
InfoWindow.WriteLine("X2=" & X2.ToString)

Dim b As Double = L3
Dim b1 As Double = PX3
Dim X3 As Double = b1
InfoWindow.WriteLine("X3=" & X3.ToString)

Dim e As Double = L4
Dim e1 As Double = R4
Dim X4 As Double = (b1 - (b1 / 180))
InfoWindow.WriteLine("X4=" & X4.ToString)

Dim c As Double = L5
Dim c1 As Double = PX5
Dim X5 As Double = c1 + 0.5 * c
InfoWindow.WriteLine("X3=" & X3.ToString)

Dim Xt As Double = ((a * X1) + (b * X3) + (c * X5) + (d * X2) + (e * X4)) / (a + b + c + d + e)
InfoWindow.WriteLine("xt=" & Xt.ToString)

Dim a11 As Double = 2 * System.Math.PI * (a + b + c + d + e)
Dim d11 As Double = 2 * System.Math.Sqrt(a11 / System.Math.PI)
InfoWindow.WriteLine("diameter=" & d11.ToString)

Dim d22 As Double = 2 * d11
Dim section As NX.Arc = Circle(0, 0, d22)
Dim axis As Vector = Vector.AxisZ
Dim length As Double = 1
Dim draft As Double = 0
Dim f As NX.Extrude = Extrude({section}, axis, length, draft)

End Sub

' Create a block for selecting a line
Private Function CreateSelectLineBlock(label As String) As UI.Block.SelectObject
Dim myBlock = New UI.Block.SelectObject
myBlock.LabelString = label
myBlock.SetFilter(Snap.NX.ObjectTypes.Type.Line)
myBlock.MaximumScope = UI.Block.SelectionScope.AnyInAssembly
Return myBlock
End Function

' Create a block for selecting an arc
Private Function CreateSelectArcBlock(label As String) As UI.Block.SelectObject
Dim myBlock = New UI.Block.SelectObject
myBlock.LabelString = label
myBlock.SetFilter(Snap.NX.ObjectTypes.Type.Arc)
myBlock.MaximumScope = UI.Block.SelectionScope.AnyInAssembly
Return myBlock
End Function

End Class

The code is giving error in external library. Here i am sending a sketch on which i am trying to run this code.
I am unable to paste a sketch here, but lemme describe in short. The sketch consist of three lines & two arcs. The 5th line is horizontal with start point 0,0,0. The third line is vertical with start point [X=16.8639045245323,Y=0,Z=2.72516199805827] & the first line is again horizontal with start point[X=19.9176149708143,Y=0,Z=29.4250720825617] & two arcs connect these lines with radius 2 & are considered as fourth line & second line. The user will select the elements in sequence 1,2,3,4,5.

I edited the code above (definitions of liney, linez were wrong). Please try again. It works for me.

I need better info about the curves, please.
Select the 5 curves, and use the Info Object function. Copy the text from the Info window and paste it here.

Lines x,y,z are the lines 1,3,5 respectively.Curve between line 1&3 is named as line 2 & curve between line 3&5 is named as line4.
L1=5
P1=( 19.9176149708143 , 0 , 29.4250720825617 )
PX1=19.9176149708143
L2=4
R2=2
L3=25
P3=( 16.8639045245323 , 0 , 2.72516199805827 )
PX3=16.8639045245323
L4=4
R4=2
L5=15
P5=( 0 , 0 , 0 )
PX5=0

Can you do what I asked, please:
Select the 5 curves, and use the NX Information-->Object function (Ctrl+I). Copy the text from the Info window and paste it here.

Information on object # 1

Name Line3
Owning part C:\Users\user\Desktop\NXOpen Try\model81.prt
Layer 1
Type Line
Color 134 (Medium Royal)
Display Color 139 (Medium Blue)
Font SOLID
Width Normal
Modified Version 20 21 May 2015 23:21 (by user user)
Created Version 20 21 May 2015 23:21 (by user user)
Information Units mm
Angle = 0.000000000
Length = 5.000000000

Start Point XC = 19.917614971 X = 19.917614971
YC = 0.000000000 Y = 0.000000000
ZC = 29.425072083 Z = 29.425072083

End Point XC = 24.917614971 X = 24.917614971
YC = 0.000000000 Y = 0.000000000
ZC = 29.425072083 Z = 29.425072083
------------------------------------------------------------
Object Dependency Graph:
Line - Line3
|___________ SKETCH_001:Sketch(3)
Used by:
Revolve(4)

------------------------------------------------------------

************************************************************

Information on object # 2

Name Arc2
Owning part C:\Users\user\Desktop\NXOpen Try\model81.prt
Layer 1
Type Arc
Color 134 (Medium Royal)
Display Color 139 (Medium Blue)
Font SOLID
Width Normal
Modified Version 20 21 May 2015 23:21 (by user user)
Created Version 20 21 May 2015 23:21 (by user user)
Information Units mm
Radius = 2.000000000
Diameter = 4.000000000
Arc Length = 4.251385396
Delta Angle = 121.793220132

Center Point XC = 18.863904525 X = 18.863904525
YC = 0.000000000 Y = 0.000000000
ZC = 27.725161998 Z = 27.725161998

Start Point XC = 19.917614971 X = 19.917614971
YC = 0.000000000 Y = 0.000000000
ZC = 29.425072083 Z = 29.425072083

End Point XC = 16.863904525 X = 16.863904525
YC = 0.000000000 Y = 0.000000000
ZC = 27.725161998 Z = 27.725161998

Rho Value 0.32722773519638
------------------------------------------------------------
Object Dependency Graph:
Arc - Arc2
|___________ SKETCH_001:Sketch(3)
Used by:
Revolve(4)

------------------------------------------------------------

************************************************************

Information on object # 3

Name Line2
Owning part C:\Users\user\Desktop\NXOpen Try\model81.prt
Layer 1
Type Line
Color 134 (Medium Royal)
Display Color 139 (Medium Blue)
Font SOLID
Width Normal
Modified Version 20 21 May 2015 23:21 (by user user)
Created Version 20 21 May 2015 23:21 (by user user)
Information Units mm
Angle = 90.000000000
Length = 25.000000000

Start Point XC = 16.863904525 X = 16.863904525
YC = 0.000000000 Y = 0.000000000
ZC = 2.725161998 Z = 2.725161998

End Point XC = 16.863904525 X = 16.863904525
YC = 0.000000000 Y = 0.000000000
ZC = 27.725161998 Z = 27.725161998
------------------------------------------------------------
Object Dependency Graph:
Line - Line2
|___________ SKETCH_001:Sketch(3)
Used by:
Revolve(4)

------------------------------------------------------------

************************************************************

Information on object # 4

Name Arc1
Owning part C:\Users\user\Desktop\NXOpen Try\model81.prt
Layer 1
Type Arc
Color 134 (Medium Royal)
Display Color 139 (Medium Blue)
Font SOLID
Width Normal
Modified Version 20 21 May 2015 23:21 (by user user)
Created Version 20 21 May 2015 23:21 (by user user)
Information Units mm
Radius = 2.000000000
Diameter = 4.000000000
Arc Length = 3.883664375
Delta Angle = 111.258788865

Center Point XC = 15.000000000 X = 15.000000000
YC = 0.000000000 Y = 0.000000000
ZC = 2.000000000 Z = 2.000000000

Start Point XC = 15.000000000 X = 15.000000000
YC = 0.000000000 Y = 0.000000000
ZC = 0.000000000 Z = 0.000000000

End Point XC = 16.863904525 X = 16.863904525
YC = 0.000000000 Y = 0.000000000
ZC = 2.725161998 Z = 2.725161998

Rho Value 0.360835973948293
------------------------------------------------------------
Object Dependency Graph:
Arc - Arc1
|___________ SKETCH_001:Sketch(3)
Used by:
Revolve(4)

------------------------------------------------------------

************************************************************

Information on object # 5

Name Line1
Owning part C:\Users\user\Desktop\NXOpen Try\model81.prt
Layer 1
Type Line
Color 134 (Medium Royal)
Display Color 139 (Medium Blue)
Font SOLID
Width Normal
Modified Version 20 21 May 2015 23:21 (by user user)
Created Version 20 21 May 2015 23:21 (by user user)
Information Units mm
Angle = 0.000000000
Length = 15.000000000

Start Point XC = 0.000000000 X = 0.000000000
YC = 0.000000000 Y = 0.000000000
ZC = 0.000000000 Z = 0.000000000

End Point XC = 15.000000000 X = 15.000000000
YC = 0.000000000 Y = 0.000000000
ZC = 0.000000000 Z = 0.000000000
------------------------------------------------------------
Object Dependency Graph:
Line - Line1
|___________ SKETCH_001:Sketch(3)
Used by:
Revolve(4)

------------------------------------------------------------

You didn't select the curves, you selected a sketch.
To select the curves (rather than the sketch) you have to set the selection filter to "Curve".

Are you still getting the error ??

still gettin an error. what changes you have made?

I changed this:

' Create three blocks to select lines
Linex = CreateSelectLineBlock("Linex")
Liney = CreateSelectLineBlock("Liney")
Linez = CreateSelectLineBlock("Linez")

Previously, it was:

' Create three blocks to select lines
Linex = CreateSelectLineBlock("Linex")
Linex = CreateSelectLineBlock("Liney")
Linex = CreateSelectLineBlock("Linez")

If you're still getting an error, show the error message, please.
Copy the error text that you can find near the end of the syslog.

The code worked great.Thank you so much

I cleaned up the code and posted the final version to the SNAP forum