Cancel
Showing results for 
Search instead for 
Did you mean: 

Make Image origin associative to Drafting Object, when placing the Image on NX Drawing Sheet....

Siemens Valued Contributor Siemens Valued Contributor
Siemens Valued Contributor

Hi -

I am trying to automate following steps in NX Drafting application... (Vb code):

 

1.Import a Image into a drawing sheets...

2.Select an existing drafting object...

3.Make the imported Image origin associative to the selected drafting object...

 

I have tried some early tests, by using the wrapper method for UF_DRF_create_image...

It works very well , but this function only allow me to place the Image on a predefined postion X Y Z....

I will ask if some of you guys, can point me in a direction, on how to use the Tag of the imported Image, and then by adding some additional code, can make the Image origin associative and connected to an existing drafting object...

 

I have attached a smal consept video, which shows the Image being placed on the sheet...and afterwards I manually make the Image origin associative ,and connected to a dimesion....But would like to also this manually step should be part of the automation....

 

Also a code snippet for placing an Image is attached....

 

Regards Lars

 

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
'Imports NXOpen.Drawings
'Imports NXOpen.Assemblies
'Imports NXOpen.PDM
'Imports System.Collections
'Imports System.Collections.Generic
'Imports NXOpen.Annotations
'Imports System.Text
'Imports NXOpen.Utilities
'Imports System.Text.RegularExpressions
'Imports System.Diagnostics
'Imports NXOpen.Routing
'Imports NXOpen.Features



Module consept_placing_image

    Dim theSession As Session = Session.GetSession()
    Dim theUI As UI = UI.GetUI()
    Dim workPart As Part = theSession.Parts.Work
    Dim displayPart As Part = theSession.Parts.Display
    Dim ufs As UFSession = UFSession.GetUFSession()
    Dim output_To_NXListingWindow As Boolean = True
    Dim output_To_NXLogFile As Boolean = True

    Sub Main()
        'ufs.Ui.ExitListingWindow() ' close windows before displaying it again
        'START HERE
        Dim imageInFile As String = "C:\OneDrive\OneDrive - \Lkl_SWP_NXOpen_API_in_use\340...SGRE_CTx_Special_Characteristic\Lkl_ver_01\Images\CTQ.png"
        Dim imageInPart As String = "CTQ"
        Dim imageTag As Tag = Tag.Null
        Dim myImageData As UFDrf.ImageData
        Dim sheetTag As Tag = Tag.Null
        Dim imagePos(2) As Double
        imagePos(0) = 290
        imagePos(1) = 360
        imagePos(2) = 0


        Try
            'image in part
            ufs.Drf.CreateImage(imageInPart, sheetTag, imagePos, imageTag)
            NX("used image in part....")
        Catch
            ufs.Drf.CreateImageFromFile(imageInFile, sheetTag, imagePos, imageTag)
            NX("used image from file....")
        End Try

        If imageTag.Equals(Tag.Null) Then
            NX("image was not imported")
        Else
            ' we ask and list some information from the newly placed image..
            ufs.Drf.AskImageData(imageTag, myImageData)
            NX("some output for debugging..")
            NX(" image height: " & myImageData.height.ToString)
            NX(" image width: " & myImageData.width.ToString)
            NX(" aspect ratio locked: " & myImageData.aspect_ratio_locked.ToString)
            NX(" name: " & myImageData.image_name)
            NX(" ImageTag: " & imageTag)
            NX(" Image file: " & imageInFile)
            NX("")
            NX("Allign method before change :" & myImageData.alignment_position)

            'here we lock the aspect ratio...
            ufs.Drf.SetImageAspectRatioLock(imageTag, True)
            'her we resize width to be 1/2 scale...
            'ufs.Drf.SetImageWidth(imageTag, myImageData.width / 2)

            'lets try play with allign position...
            Dim myAllign As New UFDrf.AlignPosition
            myAllign = UFDrf.AlignPosition.AlignMidLeft

            'change existing allign position...
            ufs.Drf.SetImageAlignPosition(imageTag, myAllign)

            'here we ask and list the updated allign position....
            ufs.Drf.AskImageData(imageTag, myImageData)
            NX("Allign method after change :" & myImageData.alignment_position)


            'lets try rotate the image 90 deg.... CCW
            'ufs.Drf.RotateImage(imageTag, 90)


        End If


    End Sub



    '=========================== NX LISTING WINDOW FUNCTION=======================================

    Public Sub NX(ByVal input As String) '' output variable listwindow - just for building VB

        If output_To_NXListingWindow Then
            Dim displayPart As Part = theSession.Parts.Display
            Dim lw As ListingWindow = theSession.ListingWindow()
            lw.Open()
            lw.WriteLine(input)
        End If

    End Sub
    '===============================================================================================

    '=========================== NX WRITE TO LOGFILE FUNCTION=======================================

    Public Sub NX_log(ByVal input As String) '' output variable listwindow - just for building VB

        If output_To_NXLogFile Then
            theSession.LogFile.WriteLine(input)
        End If

    End Sub
    '================================================================================================

    '=========================== NX UNLOAD FUNCTION ================================================

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
        'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination ' typically used in Checkmate only

    End Function
    '================================================================================================

End Module



 

 

 

 

9 REPLIES 9

Re: Make Image origin associative to Drafting Object, when placing the Image on NX Drawing Sheet...

Gears Phenom Gears Phenom
Gears Phenom

Have you recorded journal of making the picture associative to the dimension? This should give you a good starting point.

In fact you could record a journal of importing the picture too. This might give you an NXOpen solution rather than using the wrapper for UF_DRF_create_image().

Graham Inchley Snr R&D Engineer (Systems Development), Sandvik Coromant
Dell 7530 Precision, Win10, 32GB. Developing in: Java | C | KF
Production: [NX12.0.2 MP1]

Re: Make Image origin associative to Drafting Object, when placing the Image on NX Drawing Sheet...

Siemens Valued Contributor Siemens Valued Contributor
Siemens Valued Contributor

Hi Graham -

thanks anyway....

But..

Placing an Image on drawingsheet is only supported by using the UF wrapper method....

So it will not be possible to record anything.....

Also when making the Image's origin associative is not recordable...

 

/Lars

Re: Make Image origin associative to Drafting Object, when placing the Image on NX Drawing Sheet...

PLM World Member Legend PLM World Member Legend
PLM World Member Legend

Hi @LarsKlokmose 

 

I have modified the journal to set the horizontal alignment with the selected dimension. Refer the code below:

 

Option Strict Off

Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports NXOpen.Annotations

Module ImageOrigin

    Dim theSession As Session = Session.GetSession()
    Dim ufs As UFSession = UFSession.GetUFSession()
    Dim theUI As UI = UI.GetUI()
    Dim theWorkPart As Part = theSession.Parts.Work
    Dim output_To_NXListingWindow As Boolean = True
    Dim output_To_NXLogFile As Boolean = True

    Sub Main()

        Dim dimension As Tag = Tag.Null

        While select_a_dimension(dimension) = Selection.Response.Ok
            Dim dimObj As Dimension = Nothing
            Dim image As DraftingImage = Nothing

            Dim markId1 As Session.UndoMarkId = Nothing
            markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Associate")

            dimObj = NXObjectManager.Get(dimension)
            image = ImportImage()

            Dim assocOrigin1 As Annotation.AssociativeOriginData = Nothing
            assocOrigin1.OriginType = AssociativeOriginType.HorizontallyAligned
            assocOrigin1.HorizAnnotation = dimObj
            assocOrigin1.HorizAlignmentPosition = AlignmentPosition.MidCenter

           ' set the appropriate origin location as per your requirement
            image.SetAssociativeOrigin(assocOrigin1, New Point3d(273.99471282958984, 360.00000762939453, 0.0))

            Dim nErrs1 As Integer = Nothing
            nErrs1 = theSession.UpdateManager.DoUpdate(markId1)
        End While

    End Sub

    Public Function ImportImage() As Annotations.DraftingImage
        Dim image As Annotations.DraftingImage = Nothing

        'ufs.Ui.ExitListingWindow() ' close windows before displaying it again
        'START HERE
        Dim imageInFile As String = "C:\OneDrive\OneDrive - \Lkl_SWP_NXOpen_API_in_use\340...SGRE_CTx_Special_Characteristic\Lkl_ver_01\Images\CTQ.png"
        Dim imageInPart As String = "CTQ"
        Dim imageTag As Tag = Tag.Null
        Dim myImageData As UFDrf.ImageData
        Dim sheetTag As Tag = Tag.Null
        Dim imagePos(2) As Double
        imagePos(0) = 290
        imagePos(1) = 360
        imagePos(2) = 0


        Try
            'image in part
            ufs.Drf.CreateImage(imageInPart, sheetTag, imagePos, imageTag)
            NX("used image in part....")
        Catch
            ufs.Drf.CreateImageFromFile(imageInFile, sheetTag, imagePos, imageTag)
            NX("used image from file....")
        End Try

        If imageTag.Equals(Tag.Null) Then
            NX("image was not imported")
        Else
            ' we ask and list some information from the newly placed image..
            ufs.Drf.AskImageData(imageTag, myImageData)
            NX("some output for debugging..")
            NX(" image height: " & myImageData.height.ToString)
            NX(" image width: " & myImageData.width.ToString)
            NX(" aspect ratio locked: " & myImageData.aspect_ratio_locked.ToString)
            NX(" name: " & myImageData.image_name)
            NX(" ImageTag: " & imageTag)
            NX(" Image file: " & imageInFile)
            NX("")
            NX("Allign method before change :" & myImageData.alignment_position)

            'here we lock the aspect ratio...
            ufs.Drf.SetImageAspectRatioLock(imageTag, True)
            'her we resize width to be 1/2 scale...
            'ufs.Drf.SetImageWidth(imageTag, myImageData.width / 2)

            'lets try play with allign position...
            Dim myAllign As New UFDrf.AlignPosition
            myAllign = UFDrf.AlignPosition.AlignMidLeft

            'change existing allign position...
            ufs.Drf.SetImageAlignPosition(imageTag, myAllign)

            'here we ask and list the updated allign position....
            ufs.Drf.AskImageData(imageTag, myImageData)
            NX("Allign method after change :" & myImageData.alignment_position)

            'lets try rotate the image 90 deg.... CCW
            'ufs.Drf.RotateImage(imageTag, 90)

            Try
                image = CType(NXObjectManager.Get(imageTag), Annotations.DraftingImage)
            Catch ex As Exception
                theUI.NXMessageBox.Show("Drafting Image", NXMessageBox.DialogType.Error, ex.Message)
            End Try

            Return image

        End If
    End Function

    '=========================== NX LISTING WINDOW FUNCTION=======================================

    Public Sub NX(ByVal input As String) '' output variable listwindow - just for building VB

        If output_To_NXListingWindow Then
            Dim displayPart As Part = theSession.Parts.Display
            Dim lw As ListingWindow = theSession.ListingWindow()
            lw.Open()
            lw.WriteLine(input)
        End If

    End Sub
    '===============================================================================================

    '=========================== NX WRITE TO LOGFILE FUNCTION=======================================

    Public Sub NX_log(ByVal input As String) '' output variable listwindow - just for building VB

        If output_To_NXLogFile Then
            theSession.LogFile.WriteLine(input)
        End If

    End Sub
    '================================================================================================

    Function select_a_dimension(ByRef dimension As NXOpen.Tag) _
                                           As Selection.Response

        Dim message As String = "Dimension:"
        Dim title As String = "Select a dimension"
        Dim scope As Integer = UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY
        Dim response As Integer

        Dim view As NXOpen.Tag
        Dim cursor(2) As Double
        Dim ip As UFUi.SelInitFnT = AddressOf init_proc
        ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)

        Try
            ufs.Ui.SelectWithSingleDialog(message, title, scope, ip,
                         Nothing, response, dimension, cursor, view)
        Finally
            ufs.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)
        End Try

        If response <> UFConstants.UF_UI_OBJECT_SELECTED And
           response <> UFConstants.UF_UI_OBJECT_SELECTED_BY_NAME Then
            Return Selection.Response.Cancel
        Else
            Return Selection.Response.Ok
        End If

    End Function

    Function init_proc(ByVal select_ As IntPtr,
                           ByVal userdata As IntPtr) As Integer

        Dim num_triples As Integer = 1
        Dim mask_triples(0) As UFUi.Mask
        mask_triples(0).object_type = UFConstants.UF_dimension_type
        mask_triples(0).object_subtype = -1
        mask_triples(0).solid_type = 0

        ufs.Ui.SetSelMask(select_,
                           UFUi.SelMaskAction.SelMaskClearAndEnableSpecific,
                           num_triples, mask_triples)
        Return UFConstants.UF_UI_SEL_SUCCESS

    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

 

Currently the journal associate the image only in horizontal alignment and the orgin point has been set with absolute value. So do the necessary modifications required for your need.

Re: Make Image origin associative to Drafting Object, when placing the Image on NX Drawing Sheet...

Siemens Valued Contributor Siemens Valued Contributor
Siemens Valued Contributor

Hi Manikandan -

 

Thanks a lot, Thanks a  lot....

 

Looks realy good the way you did the stuff...

Unfortunately I get a small error message, when I run the code in NX....
I have attached the error message as screenshot...
And code snippets containing line numbers...
 
Seems to be something with the exceptions and the function to represent the pointer - IntPtr...
Do you succeed when executing the code.....
I'm using NX 11....
 
error message.pngline 108 .pngline 171 and line 172 .png
 
Regards Lars

Re: Make Image origin associative to Drafting Object, when placing the Image on NX Drawing Sheet...

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

Add "Imports System" to the top of the code OR fully qualify the references as System.IntPtr and System.Exception.

Re: Make Image origin associative to Drafting Object, when placing the Image on NX Drawing Sheet...

Siemens Valued Contributor Siemens Valued Contributor
Siemens Valued Contributor

Thank you so much....

I did Realy overlook this :Imports System... Smiley LOL

by adding this the problem is solved....

 

@Manikandan_R  - It works perfect now....

I can easily modify the code, so the Image always will be placed a deltadistance from the dimension...

Ask origin of selected dimension - and then transfer this X value into the  imagePos(0) array.... 

 

Regards Lars 

Re: Make Image origin associative to Drafting Object, when placing the Image on NX Drawing Sheet...

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

@LarsKlokmose ,

I'd suggest marking @Manikandan_R 's post as the solution.

Re: Make Image origin associative to Drafting Object, when placing the Image on NX Drawing Sheet...

Siemens Valued Contributor Siemens Valued Contributor
Siemens Valued Contributor

@cowski1 Yeahh - you are right...thanks

 

@Manikandan_R  it works very well this piece of code.....

however I will investigate later on why the selected dimension remains highlited, after program execution.....

Re: Make Image origin associative to Drafting Object, when placing the Image on NX Drawing Sheet...

Siemens Valued Contributor Siemens Valued Contributor
Siemens Valued Contributor

FYI 

 

I managed to get rid of the remaining highlighted objects....(Dimensions)

I simply put in an extra line:  dimObj.Unhighlight()unhighlight .png