Place part in an assemby and exit via vba program

Place part in an assemby and exit via vba program

k14348
Advocate Advocate
986 Views
12 Replies
Message 1 of 13

Place part in an assemby and exit via vba program

k14348
Advocate
Advocate

Hi,

  I have one assembly in open condition . When i run your program, it should ask file location and once i select that file, that file should come into the assembly. then program should exit. Can anybody help on this.

-karthikeyan M

0 Likes
987 Views
12 Replies
Replies (12)
Message 2 of 13

HermJan.Otterman
Advisor
Advisor

why not just use the "place" command? and close the assembly?

 

If this answers your question then please select "Accept as Solution"
Kudo's are also appreciated Smiley Wink

Succes on your project, and have a nice day

Herm Jan


0 Likes
Message 3 of 13

k14348
Advocate
Advocate

Hi,

 i am working with large assemblies. When i call large sub assemblies into main assembly its taking too much time to load. However this thing i can manage. But after placing that again one more same assembly coming when i try to clear that by using escape button its not working. Actually i need one sub assembly in the main assembly but its loading multiple . I want to avoid this. after placing first sub assy second one should not come. Its consuming more time too.

0 Likes
Message 4 of 13

Owner2229
Advisor
Advisor

Well, if you need to, you can try this (iLogic):

 

Dim oDoc As Document = ThisApplication.ActiveDocument
If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Exit Sub

Dim FB As New System.Windows.Forms.OpenFileDialog()
FB.InitialDirectory = ThisDoc.Path
FB.Filter = "*.ipt;*.iam|*.ipt;*.iam"
FB.Multiselect = True
FB.ShowDialog()
If FB.FileNames.Length = 0 Then Exit Sub

Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oMatrix As Matrix = oTG.CreateMatrix
Call oMatrix.SetToRotation(3.14159265358979 / 4, oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0, 0))
Call oMatrix.SetTranslation(oTG.CreateVector(1, 2, 1), True)

ThisApplication.SilentOperation = True
ThisApplication.ScreenUpdating = False
For Each sFile As String In FB.FileNames
    Try
        oDoc.ComponentDefinition.Occurrences.Add(sFile, oMatrix)
    Catch
    End Try
Next
ThisApplication.ScreenUpdating = True
ThisApplication.SilentOperation = False
ThisApplication.ActiveView.Update()

 

Or the same in VBA:

 

Public Sub PlaceDocument()
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
Exit Sub End If

Dim sPath As String
sPath = oDoc.FullFileName
Dim iFP As Integer
iFP = InStrRev(sPath, "\", -1)
sPath = Microsoft.VisualBasic.Left(sPath, iFP - 1)
Dim FB As New System.Windows.Forms.OpenFileDialog() FB.InitialDirectory = sPath FB.Filter = "*.ipt;*.iam|*.ipt;*.iam" FB.Multiselect = True FB.ShowDialog() If FB.FileNames.Length = 0 Then
Exit Sub End If
Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry Dim oMatrix As Matrix
Set oMatrix = oTG.CreateMatrix Call oMatrix.SetToRotation(3.14159265358979 / 4, oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0, 0)) Call oMatrix.SetTranslation(oTG.CreateVector(1, 2, 1), True) ThisApplication.SilentOperation = True ThisApplication.ScreenUpdating = False
On Error Resume Next For Each sFile As String In FB.FileNames oDoc.ComponentDefinition.Occurrences.Add(sFile, oMatrix) Next ThisApplication.ScreenUpdating = True ThisApplication.SilentOperation = False ThisApplication.ActiveView.Update()
End Sub

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 5 of 13

k14348
Advocate
Advocate

error

0 Likes
Message 6 of 13

Owner2229
Advisor
Advisor

Did you try the iLogic one? Anyway, here's the correction:

 

Public Sub PlaceDocument()
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument
If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
    Exit Sub
End If

Dim sPath As String
sPath = oDoc.FullFileName
Dim iFP As Integer
iFP = InStrRev(sPath, "\", -1)
sPath = Left(sPath, iFP - 1)
Dim FB As Inventor.FileDialog
Call ThisApplication.CreateFileDialog(FB)
FB.InitialDirectory = sPath
FB.Filter = "*.ipt;*.iam|*.ipt;*.iam"
FB.ShowOpen
If FB.FileName = vbNullString Then
    Exit Sub
End If

Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry
Dim oMatrix As Matrix
Set oMatrix = oTG.CreateMatrix
Call oMatrix.SetToRotation(3.14159265358979 / 4, oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0, 0))
Call oMatrix.SetTranslation(oTG.CreateVector(1, 2, 1), True)

ThisApplication.SilentOperation = True
ThisApplication.ScreenUpdating = False
On Error Resume Next
Call oDoc.ComponentDefinition.Occurrences.Add(FB.FileName, oMatrix)
ThisApplication.ScreenUpdating = True
ThisApplication.SilentOperation = False
ThisApplication.ActiveView.Update
End Sub
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 7 of 13

k14348
Advocate
Advocate

Debug

0 Likes
Message 8 of 13

Owner2229
Advisor
Advisor

Hey, make sure you have an active (and opened) document focused in Inventor and try running it again.

You can also try "Microsoft.VisualBasic.Left" instead.

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 9 of 13

k14348
Advocate
Advocate

Hi,

  Its working only on saved assemblies. if i open a new assembly and try to run this program its not working. This issue i can manage. But Component is placing automatically before i choose location. Do u have any solution for that? after choose my location it should be placed.

I really want to thank for your timely help. Thank u so muchSmiley Happy

0 Likes
Message 10 of 13

Owner2229
Advisor
Advisor

Alright, this would do better then. It should also work for new (unsaved) assemblies.

 

Public Sub PlaceDocument()
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument
If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Exit Sub

Dim FB As Inventor.FileDialog
Call ThisApplication.CreateFileDialog(FB)
Dim sPath As String
sPath = oDoc.FullFileName
If sPath <> vbNullString Then
    Dim iFP As Integer
    iFP = InStrRev(sPath, "\", -1)
    sPath = Left(sPath, iFP - 1)
    FB.InitialDirectory = sPath
End If
FB.Filter = "*.ipt;*.iam|*.ipt;*.iam"
FB.ShowOpen
If FB.FileName = vbNullString Then Exit Sub

Dim oCM As CommandManager
Set oCM = ThisApplication.CommandManager
Call oCM.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, FB.FileName)
Call oCM.ControlDefinitions.Item("AssemblyPlaceComponentCmd").Execute2(True)
End Sub
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 11 of 13

danijel.radenkovic
Collaborator
Collaborator

One more thing, part doesn't appear until  I hover with mouse over the imported part. Maybe it is a problem on my computer but I would like to share.

Inventor 2018/Windows 10 x64
If this information was helpful, please consider marking it as an Accepted Solution by using the Accept as Solution. Kudos are also gladly accepted.
0 Likes
Message 12 of 13

k14348
Advocate
Advocate

Hi,

  Finally u forgot the purpose of this code. Actually ur latest program works like place command. But it should not be like Place command. There should be a difference between our place program and default place command. Okay no issues. I will use the previous code of yours. At least that code met my requirement. If u can do one help means, plz open ur previous code and make correction for unsaved assemblies also. Pick place not required. Leave it as it is. once it came into the assembly i will drag it and assemble that intothe main assembly.

0 Likes
Message 13 of 13

Owner2229
Advisor
Advisor

Hi, creating a whole custom place-command would require a bit more coding and it would be pain to do so in VBA. Anyway, after placing the component you still have to constrain it, so you still have to move with it.

 

danijel.radenkovic It's because of the "ThisApplication.ScreenUpdating = False" which I used to improve the performance of Inventor while importing large assemblies.

 

Here's the original code, with the handle for new (unsaved) assemblies and without screen updating disabling:

 

Public Sub PlaceDocument()
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument
If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Exit Sub

Dim FB As Inventor.FileDialog
Call ThisApplication.CreateFileDialog(FB)
Dim sPath As String
sPath = oDoc.FullFileName
If sPath <> vbNullString Then
    Dim iFP As Integer
    iFP = InStrRev(sPath, "\", -1)
    sPath = Left(sPath, iFP - 1)
    FB.InitialDirectory = sPath
End If
FB.Filter = "*.ipt;*.iam|*.ipt;*.iam"
FB.ShowOpen
If FB.FileName = vbNullString Then Exit Sub

Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry
Dim oMatrix As Matrix
Set oMatrix = oTG.CreateMatrix
Call oMatrix.SetToRotation(3.14159265358979 / 4, oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0, 0))
Call oMatrix.SetTranslation(oTG.CreateVector(1, 2, 1), True)

ThisApplication.SilentOperation = True
On Error Resume Next
Call oDoc.ComponentDefinition.Occurrences.Add(FB.FileName, oMatrix)
ThisApplication.SilentOperation = False
End Sub

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes