Help needed to create VBA macro for creating sketch, points and punchs.

Help needed to create VBA macro for creating sketch, points and punchs.

Anonymous
Not applicable
3,246 Views
5 Replies
Message 1 of 6

Help needed to create VBA macro for creating sketch, points and punchs.

Anonymous
Not applicable

Hello, 

I have a challenge here, Need a VBA macro for following description :

1. For Creating a sketch (Need to stay in the sketch environment for importing points) 

2. Importing point from Local Excel file as there is option in the sketch "Import Sketch". 

    - To Execute the  "Import Sketch", an Excel file is required with the X,Y,Z co-ordinates in it. So this command should be included in the VBA macro, Need to specify the location of the excel file.

3. Now using above points which were created by using Import Sketch, Punch tool command should occur. 

 

Let me explain in other words,

 

I need a sketch on which multiple points needs to be added for punch tool command. 

But the challenge is importing points from the excel file.

 

Below link explains how Import Sketch command Works

 https://knowledge.autodesk.com/support/inventor-products/troubleshooting/caas/sfdcarticles/sfdcartic...

 

Hope this clears the Import Sketch. 

 

Now my requirement is to create a macro in a such way that it will create Sketch on the desired plane, then use import Sketch command..(I will be creating excel file in the system, you can specify the path for the excel file, will dump the file there.)

then after importing points on the plane, punch tool command should occur. that's it.

 

Also please see if you could allow to use more than one excel file, as there are many type of punches I use in the Model.

Its a repeat kind of work. "Just to add I am not from Coding background. Man Tongue"


thanks in advance.

 

0 Likes
Accepted solutions (1)
3,247 Views
5 Replies
Replies (5)
Message 2 of 6

FrodoZhou
Autodesk
Autodesk

Hi,

I don't think this is possible yet for we lack of the Sketch Import Points API so far.

@YuhanZhang, can you please confirm?

 

Frodo

0 Likes
Message 3 of 6

YuhanZhang
Autodesk
Autodesk

We have not the API exposed for import points onto sketch, you can log a wish to IdeaStation.

 

Now a workaround is to read the Excel data using Excel API and create the sketch points one by one(maybe cause performance slow if there are too many points data).



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 4 of 6

Anonymous
Not applicable

Hello, 

Thank you for response.

I have got something here, Not sure what API is and what it does.

 

Macro : 

"

Public Sub NewSketchOnPlane()


Dim partDoc As PartDocument
Set partDoc = ThisApplication.ActiveDocument

Dim Path1 As String
Path1 = InputBox("Enter Excel file Path e.g C:\SapWorkDir\Inventor\UpdateOpen.xlsx", "Excel file Path")

' Get the Excel file
Dim Excel As Excel.Application
Dim wb As Workbook
Dim ws As WorkSheet
Set Excel = New Excel.Application
Set wb = Excel.Workbooks.Open(Path1)
Set ws = wb.Sheets("Sheet1")


' Create a new sketch on a selected face.
Dim planarEntity As Object
Set planarEntity = ThisApplication.CommandManager.Pick(kAllPlanarEntities, _
"Select a face or work plane.")
Dim sk As Sketch
Set sk = partDoc.ComponentDefinition.Sketches.Add(planarEntity)

 

' Bring the sketch into edit mode.
sk.Edit

 

'Add a sketch point to a planar sketch
'Dim oPointOnFace As Point
'Set oPointOnFace = planarEntity.PointOnFace 'just use the PointOnFace as an example, sounds you will get user input anyway
'sk.SketchPoints.Add sk.ModelToSketchSpace(oPointOnFace)
'Enter Excel file Path


'Exit sketch edit
sk.ExitEdit

 

' Find the ButtonDefinition of Punch Tool command and execute it
Dim oDef As ButtonDefinition
Set oDef = ThisApplication.CommandManager.ControlDefinitions("SheetMetalPunchToolCmd")

 

'Execute the Punch Tool command
oDef.Execute

 

End Sub

 "

In above macro the addition is that i can give the path of the excle i want. Now the trouble is i am not able to assign the the coloumns in excel file for the points. 

Hope you are geeting what i want to say.

 

If i am on wrong path or any invalid or illigle content i am disclosing here  please let me know, i dont have any bad intentions just seeking for help. Thank you again. 

 

Regards,

Shubham.

0 Likes
Message 5 of 6

YuhanZhang
Autodesk
Autodesk
Accepted solution

Hi Shubham,

 

I update the VBA code, added the code to read data from Excel and create the sketch points, you can run it there to see if it works well:

 

Public Sub NewSketchOnPlane()

Dim partDoc As PartDocument
Set partDoc = ThisApplication.ActiveDocument

' Create a new sketch on a selected face.
Dim planarEntity As Object
Set planarEntity = ThisApplication.CommandManager.Pick(kAllPlanarEntities, _
                                                "Select a face or work plane.")
Dim sk As Sketch
Set sk = partDoc.ComponentDefinition.Sketches.Add(planarEntity)

' Bring the sketch into edit mode.
sk.Edit
 
Dim Path1 As String
Path1 = InputBox("Enter Excel file Path e.g C:\SapWorkDir\Inventor\UpdateOpen.xlsx", "Excel file Path")

If Not (ThisApplication.FileManager.FileSystemObject.fileexists(Path1)) Then
    MsgBox "The input Excel file does not exist!"
    Exit Sub
End If

' Get the Excel file
Dim Excel As Excel.Application
Dim wb As Workbook
Dim ws As WorkSheet
Set Excel = New Excel.Application
Set wb = Excel.Workbooks.Open(Path1)
Set ws = wb.Sheets(1)

 Dim iRow As Long
 iRow = 1
 
 Dim sPointX As String, sPointY As String, bStop As Boolean
 Dim oPoint As Point2d
 
 Dim oImportPts As Transaction
 Set oImportPts = ThisApplication.TransactionManager.StartTransaction(partDoc, "Import Sketch Points")
 
 ' start a transaction for import points to sketch
 Dim bAbortTransaction As Boolean: bAbortTransaction = True
 
 Do
    ' read points data from Excel
    sPointX = ws.cells(iRow, 1)
    sPointY = ws.cells(iRow, 2)
    
    Debug.Print sPointX & "," & sPointY
    
    ' create sketch point if the data is valid
    If IsNumeric(sPointX) And IsNumeric(sPointY) Then
        Set oPoint = ThisApplication.TransientGeometry.CreatePoint2d(CDbl(sPointX), CDbl(sPointY))
        sk.SketchPoints.Add oPoint, True
        bAbortTransaction = False
    Else
        bStop = True
    End If
    iRow = iRow + 1
    
 Loop Until bStop

' if no points are imported, just abort the transaction
If bAbortTransaction Then
    oImportPts.Abort
Else
    oImportPts.End
End If

'close Excel data and quit the Excel application
wb.Close
Excel.Quit

'Exit sketch edit
sk.ExitEdit

' Find the ButtonDefinition of Punch Tool command and execute it
Dim oDef As ButtonDefinition
Set oDef = ThisApplication.CommandManager.ControlDefinitions("SheetMetalPunchToolCmd")

'Execute the Punch Tool command
oDef.Execute

End Sub


If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 6 of 6

Anonymous
Not applicable

Thank you Very much, Its working so Good.

 

I really Appreciate your efforts and help.

 

Thanks you Again,

Shubham 

0 Likes