Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TugboatEng on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Journal VB, creation of cylinders in the selected points. 1

Status
Not open for further replies.

yuanma

Mechanical
Joined
Jul 15, 2014
Messages
3
Location
AT
Hello everybody. Im quite new with the use of journals. I would like locate differente cylinders taking as input selected points. I know the function select points but I can not associate it with the cylinders. I took the cylinder code just recording the journal,but I suppose it is not the correct way.
Thank you in advance.
Yua.
Option Strict Off

Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UI

Module NXJournal
Sub Main(ByVal args() As String)

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
' ----------------------------------------------
' Menu: Insert->Design Feature->Cylinder...
' ----------------------------------------------
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")
Dim nullFeatures_Feature As Features.Feature = Nothing
If Not workPart.Preferences.Modeling.GetHistoryMode Then
Throw (New Exception("Create or edit of a Feature was recorded in History Mode but playback is in History-Free Mode."))
End If

Dim cylinderBuilder1 As Features.CylinderBuilder
cylinderBuilder1 = workPart.Features.CreateCylinderBuilder(nullFeatures_Feature)
'cylinderBuilder1.BooleanOption.Type = GeometricUtilities.BooleanOperation.BooleanType.Create
'Dim targetBodies1(0) As Body
'Dim nullBody As Body = Nothing
'targetBodies1(0) = nullBody
'cylinderBuilder1.BooleanOption.SetTargetBodies(targetBodies1)
cylinderBuilder1.Diameter.RightHandSide = "5.7"
cylinderBuilder1.Height.RightHandSide = "0.2"
cylinderBuilder1.BooleanOption.Type = GeometricUtilities.BooleanOperation.BooleanType.Create
Dim targetBodies2(0) As Body
'targetBodies2(0) = nullBody
cylinderBuilder1.BooleanOption.SetTargetBodies(targetBodies2)
theSession.SetUndoMarkName(markId1, "Cylinder Dialog")
Dim unit1 As Unit
unit1 = cylinderBuilder1.Height.Units
Dim expression1 As Expression
expression1 = workPart.Expressions.CreateSystemExpressionWithUnits("0", unit1)
Dim expression2 As Expression
expression2 = workPart.Expressions.CreateSystemExpressionWithUnits("0", unit1)
Dim pointFeature1 As Features.PointFeature = CType(workPart.Features.FindObject("POINT(3)"), Features.PointFeature)

Dim myPoints As Point
If SelectPoints("Select points", myPoints) = Selection.Response.Cancel Then
Return
End If
Dim point1 As Point = myPoints 'CType(pointFeature1.FindObject("POINT 1"), Point)
Dim nullXform As Xform = Nothing
Dim point2 As Point
point2 = workPart.Points.CreatePoint(point1, nullXform, SmartObject.UpdateOption.WithinModeling)
Dim axis1 As Axis
axis1 = cylinderBuilder1.Axis
axis1.Point = point2
Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Cylinder")

theSession.DeleteUndoMark(markId2, Nothing)
Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Cylinder")
Dim nXObject1 As NXObject
nXObject1 = cylinderBuilder1.Commit()
theSession.DeleteUndoMark(markId3, Nothing)
theSession.SetUndoMarkName(markId1, "Cylinder")
Dim expression3 As Expression = cylinderBuilder1.Height
Dim expression4 As Expression = cylinderBuilder1.Diameter
cylinderBuilder1.Destroy()
workPart.Expressions.Delete(expression2)
workPart.Expressions.Delete(expression1)

' ----------------------------------------------
' Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub

Function SelectPoints(ByVal prompt As String, ByRef thePoints as Point) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim title As String = "Select points"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim selectionMask_array(0) As Selection.MaskTriple
Dim selObj() As TaggedObject

With selectionMask_array(0)
.Type = NXOpen.UF.UFConstants.UF_point_type
.Subtype = NXOpen.UF.UFConstants.UF_all_subtype
End With

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selObj)
If resp = Selection.Response.Ok Then

Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

End Module

 
The code below will create cylinders on selected points:

Code:
Option Strict Off
Imports System
Imports NXOpen

Module Module1

    Dim theSession As Session = Session.GetSession()
    Dim workPart As Part = theSession.Parts.Work

    Sub Main()

        If IsNothing(theSession.Parts.Work) Then
            'active part required
            Return
        End If

        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        Const undoMarkName As String = "cylinders on points"
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

        'declare array of tagged objects to be used for the selected points
        Dim myPoints() As TaggedObject

        'allow user to select multiple points
        If SelectPoints("Select points", myPoints) = Selection.Response.Cancel Then
            'user pressed cancel, exit journal
            Return
        End If

        'if the code gets here, points were selected
        For Each aPoint As Point In myPoints

            'create the cylinders, pass in your own diameter and height values
            CreateCylinderOnPoint(aPoint, 3.14, 6.28)

        Next

        lw.Close()

    End Sub

    Function SelectPoints(ByVal prompt As String, ByRef selObj() As TaggedObject) As Selection.Response

        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select points"
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
        Dim selectionMask_array(0) As Selection.MaskTriple

        With selectionMask_array(0)
            .Type = NXOpen.UF.UFConstants.UF_point_type
            .Subtype = NXOpen.UF.UFConstants.UF_all_subtype
        End With

        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt, _
        title, scope, selAction, includeFeatures, keepHighlighted, selectionMask_array, selObj)
        If resp = Selection.Response.Ok Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    End Function

    Sub CreateCylinderOnPoint(ByVal thePoint As Point, ByVal theDiameter As Double, ByVal theHeight As Double)
        'Create cylinder code, mostly copied and pasted from a recorded journal

        Dim nullFeatures_Feature As Features.Feature = Nothing

        Dim cylinderBuilder1 As Features.CylinderBuilder
        cylinderBuilder1 = workPart.Features.CreateCylinderBuilder(nullFeatures_Feature)

        cylinderBuilder1.BooleanOption.Type = GeometricUtilities.BooleanOperation.BooleanType.Create

        cylinderBuilder1.Diameter.RightHandSide = theDiameter.ToString

        cylinderBuilder1.Height.RightHandSide = theHeight.ToString

        'use the absolute Z vector as the direction
        Dim origin1 As Point3d = New Point3d(0.0, 0.0, 0.0)
        Dim vector1 As Vector3d = New Vector3d(0.0, 0.0, 1.0)
        Dim direction1 As Direction
        direction1 = workPart.Directions.CreateDirection(origin1, vector1, SmartObject.UpdateOption.WithinModeling)

        Dim axis1 As Axis
        axis1 = cylinderBuilder1.Axis

        axis1.Direction = direction1

        Dim nullXform As Xform = Nothing

        Dim nXObject1 As NXObject
        Dim xform1 As Xform
        xform1 = workPart.Xforms.CreateExtractXform(thePoint, SmartObject.UpdateOption.WithinModeling, False, nXObject1)

        Dim point3 As Point = CType(nXObject1, Point)

        Dim point4 As Point
        point4 = workPart.Points.CreatePoint(point3, nullXform, SmartObject.UpdateOption.WithinModeling)

        axis1.Point = point4

        Dim nXObject2 As NXObject
        nXObject2 = cylinderBuilder1.Commit()

        cylinderBuilder1.Destroy()

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image when the NX session terminates
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

        '----Other unload options-------
        'Unloads the image immediately after execution within NX
        'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

        'Unloads the image explicitly, via an unload dialog
        'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
        '-------------------------------

    End Function

End Module

www.nxjournaling.com
 
Thank you very much! I´m learning little by little
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top