Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

SW2k6 - Block Placement Macro Error

Status
Not open for further replies.

8Complex

Mechanical
Jan 28, 2004
38
I've got a macro that I am trying to create which inserts blocks at certain locations on drawings automatically depending on the size of the drawing. Eventually I need to modify it to give a choice of selections, hence the reason I don't just add it to my template.

I have no problems with A-D size drawings, however when I get to E size drawings, the X coordinate needs to be over 1.0, and SolidWorks doesn't seem to like this. If you've already selected the sheet by mouse click, it will place the block at that location, however if nothing is selected, it won't place anything.

Any ideas how to get this to work, or even suggestions as to a better method?

Here is the script as it sits:

Code:
    Public swApp                        As Object
    Public swModel                      As Object
    Public swDraw                       As Object
    Public swSheet                      As Object
    Public vSheetProps                  As Variant
    Public bRet                         As Boolean
    Public MyDoc                        As Object
    Public bPositions                   As String
    Public bGen                         As String
    Public bApp1                        As String
    Public bApp2                        As String
    Public SheetSize                    As String

Sub Main()
    Set swApp = Application.SldWorks
    Set MyDoc = swApp.ActiveDoc
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    Set swSheet = swDraw.GetCurrentSheet

    vSheetProps = swSheet.GetProperties
    SheetSize = vSheetProps(0)

    If SheetSize = "1" Then
        Debug.Print "A"
    ElseIf SheetSize = "2" Then
        Debug.Print "B"
    ElseIf SheetSize = "3" Then
        Debug.Print "C"
    ElseIf SheetSize = "4" Then
        Debug.Print "D"
    ElseIf SheetSize = "5" Then
        Debug.Print "E"
    Else
        SheetSize = MsgBox("Please use only with valid drawing sizes!", vbOKOnly, "Alert!")
        End
    End If

    '--- Write If statement to see if a General Finish is picked ---
    '--- Write General Finish to sheet ---
        If SheetSize = "1" Then
            MyDoc.SelectByID "", "SHEET", 0.183, 0.0715, 0
        ElseIf SheetSize = "2" Then
            MyDoc.SelectByID "", "SHEET", 0.398, 0.0725, 0
        ElseIf SheetSize = "3" Then
            MyDoc.SelectByID "", "SHEET", 0.532, 0.081, 0
        ElseIf SheetSize = "4" Then
            MyDoc.SelectByID "", "SHEET", 0.836, 0.079, 0
        ElseIf SheetSize = "5" Then
            MyDoc.SelectByID "", "SHEET", 1.2, 0.075, 0
        End If
        MyDoc.InsertCustomSymbol "C:\Blocks\general_finish.SLDBLK"

End Sub
 
Replies continue below

Recommended for you

I couldn't find it in the API to check, but there might be extra arguments to the InsertCustomSymbol call which would allow you to specify the location. If so you could try moving the position to that call rather than preselecting the point using the SelectByID call.

As a side note the:
SheetSize = MsgBox("Please use only with valid drawing sizes!", vbOKOnly, "Alert!")

line looks odd. In that case you are setting SheetSize to the return value of the message box. It is used in the next set of if statements, and depending on the return value of the message box, it may match one of the cases.

Eric
 
Ahhh, you are correct about the InsertCustomSymbol... that seems to be an old call, and was taken over by DrawingDoc::InsertBlock, which allows you to specify a location when placing the symbol. I will have to give it a shot, however no indication of whether or not it'll let me specify anything over the 0.999 position.

I will give the new command a shot this afternoon and re-post results. Hopefully that will solve my issues.

FWIW, the "SheetSize = MsgBox" command really only works when the returned sheet size is anything but 1-5 (A-E), so technically I should put an "End" after that point, though letting it run will just pass by any other statements since they're all based off of sheet size anyway.
 
InsertBlock does work over 0.999. Looks like next time I need to start looking into the API before just going with what I've got, and see what is the latest commands. (doh!)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor