solidworks >> macro help

by fgriffin73 » Wed, 15 Oct 2003 02:32:44 GMT

This is a macro that I found in the news group, but I get an error
when I try and run it "type mismatch" any ideas why and what can be
changed to solve it?

it happens at "sMsg = "Enter the Path to the Drawings." & vbCrLf *
"Make sure to end it with a \""

Thanks for any help

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swPart As ModelDoc2
Dim swDwg As DrawingDoc

Const swDocDrawing = 3

Sub main()
Dim sPath As String
Dim sFileSW As String
Dim sFileDWG As String
Dim sFileTitle As String

Dim sMsg As String
sMsg = "Enter the Path to the Drawings." & vbCrLf * "Make sure to
end it with a \"
sPath = InputBox(sMsg, "Enter Drawing Path", "C:\Temp\")
If sPath = "" Then Exit Sub

Set swApp = CreateObject("SldWorks.Application")

sFileSW = Dir(sPath & "*.slddrw")
Do While sFileSW <> ""
'Open Drawing - Define Titles
Set swPart = swApp.OpenDoc(sPath & sFileSW, swDocDrawing)
Set swDwg = swPart
'Save As DWG
sFileDWG = Left(sFileSW, Len(sFileSW) - 6) & "DWG"
swPart.SaveAs2 sPath & sFileDWG, 0, True, False
'Close File
sFileTitle = swPart.GetTitle
swApp.CloseDoc sFileTitle
'Next File
sFileSW = Dir
Loop
swApp.SendMsgToUser "Done!"
End Sub

solidworks >> macro help

by Brenda D. Bosley » Wed, 15 Oct 2003 03:00:44 GMT


Replace the * with & (spaces on either side of it)

--Brenda

solidworks >> macro help

by sldprt » Wed, 15 Oct 2003 06:03:52 GMT

i needed to slow down the computer for mouse clicking. i checked the
time to do this. that way you can have it run 30 frames a second

solidworks >> macro help

by rocheey » Wed, 15 Oct 2003 21:39:38 GMT

Well, there are a few things to consider here.

First off, you want to use a timer-based delay in your code; just
running a do-loop somewhere with code will vary widely between
machines.

The first obvious choice is to use the VB "Timer" control. But if you
are using VBA, or using VB with no forms, this would not be available.

If you *are* using VB, it might be easist to load an invisible form,
but there is also another API event-driven timer available; check out
the "SetTimer" and "KillTimer" API functions. This requires the
"AddressOf" operator, which is not available in VBA, but is
event-driven and can be run asychronously.

If you are using VBA, you could always use the API "GetTickCount&"
call.
This basically returns the number of milliseconds since windows was
booted. Since this is not event driven, you'll have to poll the timer
yourself until the number of milliseconds has passed. The good thing
is you dont have to deal with "Midnight rollover", which you'd have to
do with a "real" timer.

It has a Max resolution of 10ms, and seems to round 'up', so if you
ask it to wait for 1ms, it returns after 10ms. If you ask it to wait
30ms, it returns after 40ms.

' ---- snip ------------ snip -------------- snip ------------

Private Declare Function GetTickCount Lib "kernel32" () As Long

Sub main()
API_Delay 1
End Sub

Public Sub API_Delay(milliseconds As Long)

' pauses code until the number of milliseconds,
' passed as a parameter, has expired. It has a max res of 10 ms.

Dim StartTicks As Long, EndTicks As Long
Dim tickcount As Long '

StartTicks = GetTickCount
EndTicks = StartTicks + milliseconds

' Delay a minimum of milliseconds passed
Do
tickcount = GetTickCount
If tickcount > EndTicks Then Exit Do
Loop

End Sub


' ---- snip ------------ snip -------------- snip ------------

solidworks >> Macro help

by BEN EADIE » Sat, 12 Feb 2005 05:10:57 GMT

Anyone have a macro that inserts a block into a drawing at a specified X,Y
location in the sheet format?

I have tried to record a macro to do this, but no sucess. I looked up the
help in api for insterting a bloack but I cannot seem to decipher the notes
on this.

Ben

solidworks >> Macro help

by CS » Sat, 12 Feb 2005 05:34:54 GMT

use this to insert a revision block we use and it sets all of it's
annotations to and empty string.
It determines the location by the name of the template so you will have to
handle that according to your own system.

Regards
Corey


Function insertBlock() As SldWorks.BlockDefinition

Dim x As Double
Dim y As Double
Dim Bscale As Double
Dim RevLev As Object
Dim RLX As Double
Dim RLY As Double
Dim RLHeight As Double
Dim RevLevTxt As String
Dim SheetSize As String
Dim blockDef As SldWorks.BlockDefinition
Dim MyBlock As SldWorks.BlockInstance

SheetSize = LCase(Right(sheet.GetTemplateName, 8))

Select Case SheetSize
Case "a.slddrt"
x = 4.33 * 0.0254 'dimension in inches comvert to metric
y = 0.2 * 0.0254
Bscale = 1
RLX = 10.36 * 0.0254
RLY = 8.05 * 0.0254
RLHeight = 0.187 * 0.0254
Case "b.slddrt"
x = 8 * 0.0254 'dimension in inches comvert to metric
y = 0.25 * 0.0254
Bscale = 1
RLX = 13.81 * 0.0254
RLY = 10.7 * 0.0254
RLHeight = 0.187 * 0.0254
Case "c.slddrt"
x = 8.66 * 0.0254 'dimension in inches comvert to metric
y = 0.4 * 0.0254
Bscale = 2
RLX = 10.36 * 2 * 0.0254
RLY = 8.05 * 2 * 0.0254
RLHeight = 0.187 * 2 * 0.0254
Case Else
x = 0 * 0.0254 'dimension in inches comvert to metric
y = 0 * 0.0254
Bscale = 1
RLX = 10.36 * 0.0254
RLY = 8.05 * 0.0254
RLHeight = 0.187 * 0.0254
End Select
dwgdoc.EditTemplate
PathName = swApp.GetCurrentMacroPathName
PathOnly = Mid(PathName, 1, InStr(1, PathName, "A CHANGE.swp") - 2)
'dwgdoc.InsertNewNote2 RevLev, "", True, False, swNO_ARROWHEAD,
swLS_SMART, 0#, swBS_None, swBF_Tightest, 0, 0
If RevLev Is Nothing Then
RevLevTxt = ""
Set RevLev = dwgdoc.CreateText2(RevLevTxt, RLX, RLY, 0, RLHeight, 0)
RevLev.SetName "Revision Level"
End If

Set MyBlock = dwgdoc.insertBlock(PathOnly + "\A Change.SLDBLK", x, y,
0#, Bscale)

MyBlock.SetAttributeValue "TOP LETTER", ""
MyBlock.SetAttributeValue "TOP DESC", ""
MyBlock.SetAttributeValue "TOP DATE", ""
MyBlock.SetAttributeValue "MID LETTER", ""
MyBlock.SetAttributeValue "MID DESC", ""
MyBlock.SetAttributeValue "MID DATE", ""
MyBlock.SetAttributeValue "BOT LETTER", ""
MyBlock.SetAttributeValue "BOT DESC", ""
MyBlock.SetAttributeValue "BOT DATE", ""

Set blockDef = dwgdoc.GetBlockDefinition("A CHANGE")
dwgdoc.EditSheet

If blockDef Is Nothing Then
MsgBox "There was a problem finding the block." &
VBA.Chr(vbKeyReturn) & _
"Make sure " & VBA.Chr(34) & "A Change.SLDBLK" & VBA.Chr(34)
& "is in the same directory as this macro."
End
End If
Set insertBlock = blockDef

End Function
"BEN EADIE" < XXXX@XXXXX.COM > wrote in message
news:Bb9Pd.48370$tU6.13108@edtnps91...
notes



solidworks >> Macro help

by Evan T. Basalik » Tue, 22 Feb 2005 21:27:26 GMT

f the block already exists in the drawing, you can insert it using this
code:

DrawingDoc.InsertBlock("BlockName", 1 * 25.4 / 1000, 1 * 25.4 / 1000, 0, 1)

where the 1*25.4/1000 are the coordinates (in meters) where you want it.

Evan

"CS" < XXXX@XXXXX.COM > wrote in message news: XXXX@XXXXX.COM ...
0)
VBA.Chr(34)
X,Y
the



solidworks >> Macro help

by Ben Eadie » Mon, 30 Jan 2006 03:40:59 GMT

I would like to be lead in the direction of the macro code that...

The macro would:
1. open a drawing file
2. complete some tasks I have written code for
3. close and save the drawing
4. cycle through all the drawings in the folder and complete tasks 1 thru 3

Does anyone have a macro that does this or can lead me into the code to
do such a thing? Also things I would like to be able to do with this
code, would be to be able to open parts, and or assemblies, and or
drawings and any combination of the above

Ben

solidworks >> Macro help

by TOP » Mon, 30 Jan 2006 04:02:31 GMT

Strangely enough that was presented at SWW2006. Earl Hasz was the
presenter. You will need a C# compiler and .NET 2.2 to get it going.
I'll try to remember to let you know when it is available.

solidworks >> Macro help

by Tony » Mon, 30 Jan 2006 07:32:42 GMT

My guess is that Earl presented this in C# because .Net is the future. But
this can be done in VB and Windows Scripting Host. Use the WSH files
collection and then set a ModelDoc2 reference to the current file. Open the
file in SW, make your changes and use ModelDoc2.Save and .Close. Repeat for
each file.

Hope this helps.

Tony

solidworks >> Macro help

by That70sTick » Mon, 30 Jan 2006 07:46:48 GMT

Everything listed can be done w/ standard VBA macro. No need for
compiled code. You will need to branch out a bit from stock macro
template and work w/ DSOfile object to sort through file folders.

Also, VBA does not grant license to use "Common Dialog" for file
open/save control or folder browser control. These has to be accessed
via Windows API calls.

solidworks >> Macro help

by Ben Eadie » Mon, 30 Jan 2006 10:20:25 GMT

I am not much of a coder, I pretty much hack and paste what I need from
other macros that I could not create on my own. So does anyone have some
example code I can use.

I really want to take a API course but our local resellers do not seem
to have the people or inclination to have this course. Any on line
courses that anyone knows of so that I can go out on my own and just
start coding without annoying everyone? I have a little VBA under my
belt so I am hoping there is enough base there to work on.

Ben

solidworks >> Macro help

by stevefye » Mon, 30 Jan 2006 12:59:36 GMT

You have the API code for that?(the File-Open routine usually called by
the Common-Dialog Control)? That would be awesome.

For the original poster, yes, standard VB will do what you're wanting.
I've already written code that will parse a directory for a quick
batch-print. Let me know what you intend to do to the drawings once
you have opened them.

solidworks >> Macro help

by That70sTick » Mon, 30 Jan 2006 13:10:27 GMT

< http://www.mentalis.org/index2.shtml> ;

API calls to get components not directly licensed.
Perfectly legit.

solidworks >> Macro help

by Tin Man » Mon, 30 Jan 2006 20:35:36 GMT

There's a macro at http://www.nhcad.com/sw_macros/ called BATCH
OPEN.SWB that will open all the files in a specified directory (one at
a time), perform a function, then save and close. The code is setup to
open parts and change their density, but it can easily be modified to
open assemblies and drawings.

Ken

Similar Threads

1. Macro help

2. macro - help

Hi,
I was wandering If anyone can create macro, which will change color
from AC to MS?
e.g. I have file from AC with default color table. When I save file as
*.dgn I must used color.tbl from MS.
After I have one file from AC which was converted to MS but all
colours are different.
Who can create macro or knows easy way to create new color table for
AC?

Please let me know asap :-)

Zuza

3. Macro Help

4. New Drawing Macro Help

I've got a macro question, I'm trying to make a macro that creates a
new drawing file from an assembly file, I am then wanting it to
generate various views placed in the same place.  The last couple of
lines are the problem, this macro places views of a certain file but if
I open up a different assembly and make a drawing it places the views
from the first assembly.  Any help would be greatly appreciated.  I
know very little about Visual Basic, I'm currently taking a class in it
but I haven't learned much yet.  I'm looking for the command to use
that selects the currently opened file name and puts it as a variable
to use in the drawing view creation.  Also is there any way to make a
macro to do the selected dimension.  For example a macro to change a
dimension that is selected to mm from inches?

'
******************************************************************************
' D:\DOCUME~1\LUCASL~1\LOCALS~1\Temp\swx4356\Macro1.swb - macro
recorded on 05/28/06 by *********************
'
******************************************************************************
Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object

Sub main()

Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set SelMgr = Part.SelectionManager
swApp.ActiveDoc.ActiveView.FrameLeft = 0
swApp.ActiveDoc.ActiveView.FrameTop = 0
swApp.ActiveDoc.ActiveView.FrameState = 1
Set Part = swApp.NewDocument("Drawing Template File Path", 12, 0.2794,
0.4318)
Set Part = swApp.ActiveDoc
Set SelMgr = Part.SelectionManager
Part.ViewZoomtofit2
boolstatus = Part.ActivateView("Drawing View1")
boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW",
0.1173527476636, 0.1787637757009, 0, False, 0, Nothing, 0)
Part.EditDelete
Dim DrawView As Object
Set DrawView = Part.CreateDrawViewFromModelView2("Assembly File Path",
"*Isometric", 0.1297821308411, 0.1627831401869, 0)
boolstatus = Part.ActivateView("Drawing View4")
Set DrawView = Part.CreateDrawViewFromModelView2("Assebly File Path",
"*Front", 0.294915364486, 0.1610075140187, 0)
Part.ClearSelection2 True
End Sub

Lucas Laverman
Senior, Mechanical Engineering

5. MAcro Help Please!

6. Select chain and convert macro help

If you play the macro you will notice that it only selects the one segment.
I couldn't find an API to do this you may have to get all the segments and
compare endpoints to do this a bit more intensive.

Corey

"Eric T." < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...
> Can someone show me how to change the API call to reflect a more
> generic macro?
> What I need is a simple macro to select chain ref. sketch geom. under
> the mouse and convert it entities. when I run the macro recorder I
> get:
>
> Dim swApp As Object
> Dim Part As Object
> Dim boolstatus As Boolean
> Dim longstatus As Long, longwarnings As Long
> Dim FeatureData As Object
> Dim Feature As Object
> Dim Component As Object
> Sub main()
>
> Set swApp = Application.SldWorks
>
> Set Part = swApp.ActiveDoc
> boolstatus = Part.Extension.SelectByID2("Line31@Sketch7",
> "EXTSKETCHSEGMENT", 0.02657237821555, -0.04846803192663, 0, False, 0,
> Nothing)
> boolstatus = Part.SketchUseEdge2(False)
> Part.ClearSelection2 True
> End Sub
>
>
> Not sure how to clean this up and just work with what under the mouse.
> Any help would be great.
>
> Eric


7. API: Select chain and convert macro help

8. simple macro help

I've used the macro feature a couple of times before but never got into
programming with code.

I have created a macro that rotates the part to an isometric view, zooms it
to fit the screen and saves it to a specific location.



'
****************************************************************************
**
' C:\DOCUME~1\lucas\LOCALS~1\Temp\swx1772\Macro1.swb - macro recorded on
08/26/05 by lucas
'
****************************************************************************
**
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim FeatureData As Object
Dim Feature As Object
Dim Component As Object
Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
Part.ShowNamedView2 "*Isometric", 7
Part.ViewZoomtofit2
Part.SaveAs2 "E:\Lucas\Seradex Items\Part Pictures\11115.JPG", 0, True,
False
End Sub



As you can see, the part is always saved under the name "11115.JPG". I would
like to change it so that the file name minus the ".SLDPRT" is used.

Can anyone help me out?

Thanks