Submitted by anant on Sun, 05/17/2015 - 04:30
Forums:
Hello there,
I have block with simple hole in it on my NX user interface.I have successfully extracted hole diameter & exported the diameter to text file. I am trying a journal which will do the things in following sequence.
1.extract hole diameter from block
2.export it to text file.
3.delete the block(workpart)
4.create a circle by importing the diameter from the above mentioned text file.
can you help?????
re: import data to create geometry
At what step are you stuck?
importing data
i am struggling with importing the data from text file. can you send me any routine which can import the data & create circle
re: importing data
There are many possible ways to do what you want, below is but one approach. There are two journals below; the first will look through the feature list for the first BLOCK feature. If it finds a block it will look for a hole feature in the block; it will inspect the hole parameters and write some information to a text file. The second journal will read this text file and create a circle in the work part. Both use a constant named "fileName"; change the value of this constant to something valid for your file system and make sure the value matches in both journals.
Journal to write information to file:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Module write_info
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
Sub Main()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If
lw.Open()
Const undoMarkName As String = "write circle info"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
'change file name as needed before running journal
Const fileName As String = "C:\temp\circle.txt"
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Dim myBlock As Features.BodyFeature = Nothing
'find first block feature
For Each temp As Features.Feature In workPart.Features
If temp.FeatureType = "BLOCK" Then
myBlock = temp
Exit For
End If
Next
If IsNothing(myBlock) Then
lw.WriteFullline("no block found")
Return
End If
'find first hole feature in the block
Dim blockChildren() As Features.Feature = myBlock.GetAllChildren
Dim blockHole As Features.HolePackage = Nothing
For Each temp As Features.Feature In blockChildren
If temp.FeatureType = "HOLE PACKAGE" Then
blockHole = temp
Exit For
End If
Next
If IsNothing(blockHole) Then
lw.WriteFullline("no hole found in block")
Return
End If
Dim location As Point3d = blockHole.Location
Dim diameter As Double = 0
Dim holeDirections() As Vector3d = Nothing
Dim theHolePackageBuilder As Features.HolePackageBuilder = workPart.Features.CreateHolePackageBuilder(blockHole)
'lw.WriteFullline(theHolePackageBuilder.Type.ToString)
Select Case theHolePackageBuilder.Type
Case Is = Features.HolePackageBuilder.Types.GeneralHole
diameter = theHolePackageBuilder.GeneralSimpleHoleDiameter.Value
Case Is = Features.HolePackageBuilder.Types.DrillSizeHole
diameter = theHolePackageBuilder.DrillSizeHoleDiameter.Value
Case Is = Features.HolePackageBuilder.Types.ScrewClearanceHole
diameter = theHolePackageBuilder.ScrewClearanceHoleDiameter.Value
Case Is = Features.HolePackageBuilder.Types.ThreadedHole
diameter = theHolePackageBuilder.TapDrillDiameter.Value
Case Is = Features.HolePackageBuilder.Types.HoleSeries
diameter = theHolePackageBuilder.StartHoleData.HoleDiameter.Value
Case Else
lw.WriteFullline("error: unsupported hole type")
Return
End Select
theHolePackageBuilder.Destroy()
blockHole.GetDirections(holeDirections)
'write the info to a file
Try
WriteToFile(fileName, location, holeDirections(0), diameter)
Catch ex As Exception
lw.WriteFullline("error writing to file")
Return
End Try
lw.WriteFullline("circle information written to file: " & fileName)
lw.Close()
End Sub
Sub WriteToFile(ByVal theFile As String, ByVal theLocation As Point3d, ByVal theDirection As Vector3d, ByVal theDiameter As Double)
If IO.File.Exists(theFile) Then
'delete it
Try
IO.File.Delete(theFile)
Catch ex As Exception
Throw ex
End Try
End If
Using myWriter As New IO.StreamWriter(theFile, False)
myWriter.WriteLine(theLocation.X.ToString & ", " & theLocation.Y.ToString & ", " & theLocation.Z.ToString & ", " & _
theDirection.X.ToString & ", " & theDirection.Y.ToString & ", " & theDirection.Z.ToString & ", " & _
theDiameter.ToString)
End Using
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
End Module
Journal to read information and create circle:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Module read_info
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
Sub Main()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If
lw.Open()
Const undoMarkName As String = "read circle info"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
'change file name as needed before running journal
Const fileName As String = "C:\temp\circle.txt"
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Dim circleLocation As Point3d
Dim circleNormal As Vector3d
Dim circleDiameter As Double
'open file, retrieve data
If Not IO.File.Exists(fileName) Then
lw.WriteFullline("file not found: " & fileName)
Return
End If
Try
ReadFile(fileName, circleLocation, circleNormal, circleDiameter)
Catch ex As Exception
lw.WriteFullline("error reading file")
Return
End Try
Dim circleXVec As Vector3d = Xdir(circleNormal)
Dim circleCenter As Point = workPart.Points.CreatePoint(circleLocation)
Dim circleZDir As Direction = workPart.Directions.CreateDirection(circleCenter, circleNormal)
Dim cScale As Scalar = workPart.Scalars.CreateScalar(1, Scalar.DimensionalityType.Length, SmartObject.UpdateOption.WithinModeling)
Dim circleXform As Xform = workPart.Xforms.CreateXform(circleCenter, XYZAxis.ZAxis, circleZDir, cScale, SmartObject.UpdateOption.WithinModeling)
Dim circleMatrix As NXMatrix = workPart.NXMatrices.Create(circleXform.Orientation)
workPart.Curves.CreateArc(circleLocation, circleMatrix, circleDiameter / 2, 0, Math.PI * 2)
lw.Close()
End Sub
Sub ReadFile(ByVal theFile As String, ByRef location As Point3d, ByRef direction As Vector3d, ByRef diameter As Double)
Dim delim As Char() = {","c}
Dim line As String
Using sr As IO.StreamReader = New IO.StreamReader(theFile)
Try
line = sr.ReadLine()
While Not line Is Nothing
Dim strings As String() = line.Split(delim)
location.X = Double.Parse(strings(0))
location.Y = Double.Parse(strings(1))
location.Z = Double.Parse(strings(2))
direction.X = Double.Parse(strings(3))
direction.Y = Double.Parse(strings(4))
direction.Z = Double.Parse(strings(5))
diameter = Double.Parse(strings(6))
line = sr.ReadLine()
End While
Catch ex As Exception
Throw ex
End Try
End Using
End Sub
Function Xdir(ByVal Zdir As Vector3d) As Vector3d
Dim xvec(2) As Double
Dim zvec(2) As Double
zvec(0) = Zdir.X
zvec(1) = Zdir.Y
zvec(2) = Zdir.Z
theUfSession.Vec3.AskPerpendicular(zvec, xvec)
Dim temp As Vector3d
temp.X = xvec(0)
temp.Y = xvec(1)
temp.Z = xvec(2)
Return temp
End Function
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
End Module
Worked
That worked great. Thanks alot....
Obtaining Circle diameter
Hey there,
I have drawn a circle & extruded it to certain height. I want a journal to get the diameter. please send the code
re: circle diameter
Find the circle of interest, cast it to the type "Arc" (if needed) and use the .Radius property to get the radius value.