SolidWorks机械工程师网——最大的SolidWorks学习平台

 找回密码
 立即注册

QQ登录

只需一步,快速开始

好品数字
好品数字
查看: 10113|回复: 51
打印 上一主题 下一主题

新人求教2个宏合并

  [复制链接]

8

主题

58

帖子

24

金币

侠客

Rank: 3Rank: 3Rank: 3

积分
400

最佳新人活跃会员热心会员宣传达人

跳转到指定楼层
楼主
 楼主| 发表于 2021-8-1 16:48:24 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
这个宏是图号分离的:


'定义solidwork

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

Dim a As Integer

Dim b As String

Dim m As String

Dim e As String

Dim k As String

Dim t As String

Dim c As String

Dim j As Integer

Dim strmat As String

Dim tempvalue As String

Sub main()

'link solidworks

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Set SelMgr = Part.SelectionManager

swApp.ActiveDoc.ActiveView.FrameState = 1

'设定变量

c = swApp.ActiveDoc.GetTitle() '零件名

strmat = Chr(34) + Trim("SW-Material" + "@") + c + Chr(34)

blnretval = Part.DeleteCustomInfo2("", "代号")

blnretval = Part.DeleteCustomInfo2("", "名称")

blnretval = Part.DeleteCustomInfo2("", "材料")

a = InStr(c, " ") - 1      '重点:分隔标识符,这里是一个空格

If a > 0 Then

    k = Left(c, a)

    t = Left(LTrim(e), 3)

    If t = "GBT" Then

        e = "GB/T" + Mid(k, 4)

    Else

        e = k

    End If

    b = Mid(c, a + 2)

    t = Right(c, 7)

    If t = ".SLDPRT" Or t = ".SLDASM" Then

        j = Len(b) - 7

    Else

        j = Len(b)

    End If

    m = Left(b, j)

End If

blnretval = Part.AddCustomInfo3("", "代号", swCustomInfoText, e)  '代号

blnretval = Part.AddCustomInfo3("", "名称", swCustomInfoText, m)  '名称

blnretval = Part.AddCustomInfo3("", "表面处理", swCustomInfoText, " ")

End Sub







这个宏是获取最大外形尺寸的:
Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Dim Height As Variant
Dim Width As Variant
Dim Length As Variant
Dim Corners As Variant
Dim retval As Boolean
Dim UserUnits As Variant
Const swDocPart = 1
Const swDocASSEMBLY = 2
'Enum swLengthUnit_e
Const swMM = 0
Const swCM = 1
Const swMETER = 2
Const swINCHES = 3
Const swFEET = 4
Const swFEETINCHES = 5
Const swANGSTROM = 6
Const swNANOMETER = 7
Const swMICRON = 8
Const swMIL = 9
Const swUIN = 10
'Enum swFractionDisplay_e
Const swNONE = 0
Const swDECIMAL = 1
Const swFRACTION = 2

Dim swCustPropMgr As SldWorks.CustomPropertyManager
Dim swModel As SldWorks.ModelDoc2
Dim StockSize As String
Function DecimalToFeetInches(DecimalLength As Variant, Denominator As Integer) As String
  ' converts decimal inches to feet/inches/fractions
  Dim intFeet As Integer
  Dim intInches As Integer
  Dim intFractions As Integer
  Dim FractToDecimal As Double
  Dim remainder As Double
  Dim tmpVal As Double

  ' compute whole feet
  intFeet = Int(DecimalLength / 12)
  remainder = DecimalLength - (intFeet * 12)
  tmpVal = CDbl(Denominator)

  intInches = Int(remainder)
  remainder = remainder - intInches

  If Not (remainder = 0) Then
    If Not (Denominator = 0) Then
      FractToDecimal = 1 / tmpVal
        If FractToDecimal > 0 Then
          intFractions = Int(remainder / FractToDecimal)
          If (remainder / FractToDecimal) - intFractions > 0 Then
            intFractions = intFractions + 1
          End If
        End If
     End If
  End If
  Call FractUp(intFeet, intInches, intFractions, Denominator) ' Simplify up & down

  DecimalToFeetInches = LTrim$(Str$(intFeet)) & "'-"
  DecimalToFeetInches = DecimalToFeetInches & LTrim$(Str$(intInches))
  If intFractions > 0 Then
    DecimalToFeetInches = DecimalToFeetInches & " "
    DecimalToFeetInches = DecimalToFeetInches & LTrim$(Str$(intFractions))
    DecimalToFeetInches = DecimalToFeetInches & "/" & LTrim$(Str$(Denominator))
  End If

  DecimalToFeetInches = DecimalToFeetInches & Chr$(34)

End Function
Function FractUp(InputFt As Integer, InputInch As Integer, InputNum As Integer, InputDenom As Integer)

  While InputNum Mod 2 = 0 And InputDenom Mod 2 = 0
    InputNum = InputNum / 2
    InputDenom = InputDenom / 2
  Wend

  If InputDenom = 1 Then  ' Full inch
    InputInch = InputInch + 1
    InputNum = 0
    If InputInch = 12 Then  ' Full foot
      InputFt = InputFt + 1
      InputInch = 0
    End If
  End If
End Function
'---------------------------------------------------------------------------------
Sub SortDimensions()
    Dim arr(1 To 3) As Double
    arr(1) = Length
    arr(2) = Width
    arr(3) = Height
    SortArr arr
End Sub

Sub SortArr(arr() As Double)
    Dim i As Long
    Dim j As Long
    Dim tmp As String
    Dim p As String

    For i = LBound(arr) To UBound(arr) - 1
        For j = i + 1 To UBound(arr)
            If arr(i) > arr(j) Then
                tmp = arr(i)
                arr(i) = arr(j)
                arr(j) = tmp
            End If
        Next j
    Next i
    '---------------------------------------------------------------------------------
   MsgBox (arr(1) & " x " & arr(2) & " x " & arr(3))
   StockSize = (arr(1) & " x " & arr(2) & " x " & arr(3))


Set swCustProp = Part.Extension.CustomPropertyManager("")
retval = Part.DeleteCustomInfo2("", "外形尺寸") 'Remove existing properties
swCustProp.Add3 "外形尺寸", swCustomInfoText, StockSize, 1 'Add latest values
   '---------------------------------------------------------------------------------
End Sub
Sub Main()
Dim arr(1 To 3) As Double

Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc

If Part Is Nothing Then     ' Did we get anything?
  MsgBox "You need to have a part or assy open at this point." & Chr$(13) & Chr$(10) _
          & Chr$(10) & "Open one and try again."
  Exit Sub
End If
If (Part.GetType = swDocPart) Then
  Corners = Part.GetPartBox(True)         ' True comes back as system units - meters
ElseIf Part.GetType = swDocASSEMBLY Then  ' Units will come back as meters
  Corners = Part.GetBox(0)
Else
  MsgBox "This macro is only useful with a part or assy." & Chr$(13) & Chr$(10) & "Open one of those and try again."
  Exit Sub
End If
UserUnits = Part.GetUnits()
Select Case Part.GetUnits(0)
  Case swMM
    ConvFactor = 1 * 1000
  Case swCM
    ConvFactor = 1 * 100
  Case swMETER
    ConvFactor = 1
  Case swINCHES
    ConvFactor = 1 / 0.0254
  Case swFEET
    ConvFactor = 1 / (0.0254 * 12)
  Case swFEETINCHES
    ConvFactor = 1 / 0.0254  ' Pass inches through
  Case swANGSTROM
    ConvFactor = 10000000000#
  Case swNANOMETER
    ConvFactor = 1000000000
  Case swMICRON
    ConvFactor = 1000000
  Case swMIL
    ConvFactor = (1 / 0.0254) * 1000
  Case swUIN
    ConvFactor = (1 / 0.0254) * 1000000
End Select
Height = Round((Abs(Corners(4) - Corners(1)) * ConvFactor), UserUnits(3)) ' Z axis
Width = Round((Abs(Corners(5) - Corners(2)) * ConvFactor), UserUnits(3))  ' Y axis
Length = Round((Abs(Corners(3) - Corners(0)) * ConvFactor), UserUnits(3)) ' X axis
' Check for either (Feet-Inches OR Inches) AND fractions.  If so, return Ft-In
If (UserUnits(0) = 5 Or UserUnits(0) = 3) And UserUnits(1) = 2 Then
  Height = DecimalToFeetInches(Height, Val(UserUnits(2)))
  Width = DecimalToFeetInches(Width, Val(UserUnits(2)))
  Length = DecimalToFeetInches(Length, Val(UserUnits(2)))
End If
Call SortDimensions

End Sub







因为是新手 所以这两个宏我都要一个一个的运行,所以还望大神出手 帮我 把这两个宏弄成一个,让我一下就可以运行这两个宏,,多谢多谢,
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享教程|习题|模型|技巧 点赞点赞 拍砖拍砖
SolidWorks机械工程师网
提示:建议使用谷歌浏览器浏览本网站!如单击这里下载!否则,可能无法下载附件文件!(支持大多数版本的谷歌浏览器,支持360和QQ浏览器的极速模式,即谷歌内核模式,使用IE和Edge浏览器,浏览个别网页以及下载文件时,会误报“***不安全”,此时需要单击“继续访问此不安全站点(不推荐)”才可以继续下载,另外,本网站不含任何不安全的文件,已联系微软公司解决,纯属IE和Edge浏览器误报)
回复

使用道具 举报

37

主题

990

帖子

1万

金币

版主

Rank: 7Rank: 7Rank: 7Rank: 7

积分
19504

最佳新人活跃会员热心会员宣传达人灌水之王突出贡献

推荐
发表于 2021-8-5 09:31:57 | 只看该作者
提供一个简单的办法——逐个点名运行(即在原来两个宏的同一文件夹下,新增加一个点名宏)。
具体需要你自己动手更改一下,如下图所示,要自行更改文件名,还有宏的名称。



如果还不能理解的话,建议看一看相关的API的说明,就是点中RunMacro,然后按F1。


其实代码都已经贴出来了,不过还是提供个附件吧
点名宏_2021-08-05.zip (4.95 KB, 下载次数: 250, 售价: 1 金币)



SolidWorks机械工程师网
提示:建议使用谷歌浏览器浏览本网站!如单击这里下载!否则,可能无法下载附件文件!(支持大多数版本的谷歌浏览器,支持360和QQ浏览器的极速模式,即谷歌内核模式,使用IE和Edge浏览器,浏览个别网页以及下载文件时,会误报“***不安全”,此时需要单击“继续访问此不安全站点(不推荐)”才可以继续下载,另外,本网站不含任何不安全的文件,已联系微软公司解决,纯属IE和Edge浏览器误报)
回复 支持 2 反对 0

使用道具 举报

0

主题

42

帖子

61

金币

侠客

Rank: 3Rank: 3Rank: 3

积分
469

最佳新人活跃会员宣传达人

推荐
发表于 2022-9-5 10:34:46 | 只看该作者
SW机械工程师网,找到组织了!
SolidWorks机械工程师网
回复 支持 1 反对 0

使用道具 举报

2

主题

305

帖子

79

金币

长老

Rank: 6Rank: 6Rank: 6

积分
1096

最佳新人活跃会员热心会员宣传达人灌水之王突出贡献

推荐
发表于 2022-8-12 15:14:03 | 只看该作者
感谢楼主分享,很不错!
SolidWorks机械工程师网
回复 支持 1 反对 0

使用道具 举报

0

主题

283

帖子

156

金币

堂主

Rank: 4

积分
935

最佳新人活跃会员热心会员宣传达人灌水之王突出贡献

推荐
发表于 2022-7-14 22:23:07 | 只看该作者

   经典案例图书
SW机械工程师网,找到组织了!
SolidWorks机械工程师网
回复 支持 1 反对 0

使用道具 举报

8

主题

58

帖子

24

金币

侠客

Rank: 3Rank: 3Rank: 3

积分
400

最佳新人活跃会员热心会员宣传达人

推荐
 楼主| 发表于 2021-10-22 09:22:41 | 只看该作者
design100 发表于 2021-8-5 10:16
'定义solidwork

'这个宏是获取最大外形尺寸的:

感谢 感谢!谢谢出手
SolidWorks机械工程师网
回复 支持 1 反对 0

使用道具 举报

0

主题

94

帖子

102

金币

长老

Rank: 6Rank: 6Rank: 6

积分
1578

最佳新人活跃会员热心会员宣传达人

推荐
发表于 2021-9-24 09:52:42 | 只看该作者

   经典案例图书
感谢楼主分享,很不错!
SolidWorks机械工程师网
回复 支持 1 反对 0

使用道具 举报

1

主题

296

帖子

121

金币

长老

Rank: 6Rank: 6Rank: 6

积分
1822

最佳新人活跃会员热心会员宣传达人灌水之王突出贡献

推荐
发表于 2021-9-11 13:57:21 | 只看该作者
感谢楼主分享,很不错!
SolidWorks机械工程师网
回复 支持 1 反对 0

使用道具 举报

19

主题

127

帖子

77

金币

长老

Rank: 6Rank: 6Rank: 6

积分
1369

最佳新人活跃会员热心会员宣传达人灌水之王

推荐
发表于 2021-8-15 16:33:26 | 只看该作者
Allate 发表于 2021-8-5 09:31
提供一个简单的办法——逐个点名运行(即在原来两个宏的同一文件夹下,新增加一个点名宏)。
具体需要你自 ...

这个方法好,点赞
SolidWorks机械工程师网
回复 支持 1 反对 0

使用道具 举报

8

主题

58

帖子

24

金币

侠客

Rank: 3Rank: 3Rank: 3

积分
400

最佳新人活跃会员热心会员宣传达人

推荐
 楼主| 发表于 2021-8-11 14:41:26 来自手机 | 只看该作者
谢谢,这几天在出差,回去了试试!
SolidWorks机械工程师网
回复 支持 1 反对 0

使用道具 举报

8

主题

58

帖子

24

金币

侠客

Rank: 3Rank: 3Rank: 3

积分
400

最佳新人活跃会员热心会员宣传达人

11#
 楼主| 发表于 2021-8-1 16:49:35 | 只看该作者
如果能简单出个教程 告诉我怎么合并,当然更加好,感谢  感谢
SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

0

主题

6

帖子

11

金币

混混

Rank: 1

积分
33
QQ
12#
发表于 2021-8-2 12:16:12 | 只看该作者
顶一下,坐等高手!
SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

8

主题

58

帖子

24

金币

侠客

Rank: 3Rank: 3Rank: 3

积分
400

最佳新人活跃会员热心会员宣传达人

13#
 楼主| 发表于 2021-8-3 11:25:42 来自手机 | 只看该作者
来个高手  帮忙弄一下,感谢!
SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

27

主题

1348

帖子

3136

金币

传奇

Rank: 8Rank: 8

积分
10382

最佳新人活跃会员热心会员宣传达人灌水之王突出贡献

14#
发表于 2021-8-5 10:14:16 | 只看该作者
Sub Main()
程序1
'End Sub'停此句
'Sub Main()'停此句
程序2
End Sub

放到一起不就行了吗
SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

27

主题

1348

帖子

3136

金币

传奇

Rank: 8Rank: 8

积分
10382

最佳新人活跃会员热心会员宣传达人灌水之王突出贡献

15#
发表于 2021-8-5 10:16:05 | 只看该作者
加入QQ群
参与讨论和学习

SolidWorks技术交流群

或扫描二维码加入

'定义solidwork

'这个宏是获取最大外形尺寸的:
Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Dim Height As Variant
Dim Width As Variant
Dim Length As Variant
Dim Corners As Variant
Dim retval As Boolean
Dim UserUnits As Variant
Const swDocPart = 1
Const swDocASSEMBLY = 2
'Enum swLengthUnit_e
Const swMM = 0
Const swCM = 1
Const swMETER = 2
Const swINCHES = 3
Const swFEET = 4
Const swFEETINCHES = 5
Const swANGSTROM = 6
Const swNANOMETER = 7
Const swMICRON = 8
Const swMIL = 9
Const swUIN = 10
'Enum swFractionDisplay_e
Const swNONE = 0
Const swDECIMAL = 1
Const swFRACTION = 2

Dim swCustPropMgr As SldWorks.CustomPropertyManager
Dim swModel As SldWorks.ModelDoc2
Dim StockSize As String

'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

Dim a As Integer

Dim b As String

Dim m As String

Dim e As String

Dim k As String

Dim t As String

Dim c As String

Dim j As Integer

Dim strmat As String

Dim tempvalue As String



Sub Main()

'link solidworks

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Set SelMgr = Part.SelectionManager

swApp.ActiveDoc.ActiveView.FrameState = 1

'设定变量

c = swApp.ActiveDoc.GetTitle() '零件名

strmat = Chr(34) + Trim("SW-Material" + "@") + c + Chr(34)

blnretval = Part.DeleteCustomInfo2("", "代号")

blnretval = Part.DeleteCustomInfo2("", "名称")

blnretval = Part.DeleteCustomInfo2("", "材料")

a = InStr(c, " ") - 1      '重点:分隔标识符,这里是一个空格

If a > 0 Then

    k = Left(c, a)

    t = Left(LTrim(e), 3)

    If t = "GBT" Then

        e = "GB/T" + Mid(k, 4)

    Else

        e = k

    End If

    b = Mid(c, a + 2)

    t = Right(c, 7)

    If t = ".SLDPRT" Or t = ".SLDASM" Then

        j = Len(b) - 7

    Else

        j = Len(b)

    End If

    m = Left(b, j)

End If

blnretval = Part.AddCustomInfo3("", "代号", swCustomInfoText, e)  '代号

blnretval = Part.AddCustomInfo3("", "名称", swCustomInfoText, m)  '名称

blnretval = Part.AddCustomInfo3("", "表面处理", swCustomInfoText, " ")

'End Sub'停此句
'Sub Main()'停此句

Dim arr(1 To 3) As Double

Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc

If Part Is Nothing Then     ' Did we get anything?
  MsgBox "You need to have a part or assy open at this point." & Chr$(13) & Chr$(10) _
          & Chr$(10) & "Open one and try again."
  Exit Sub
End If
If (Part.GetType = swDocPart) Then
  Corners = Part.GetPartBox(True)         ' True comes back as system units - meters
ElseIf Part.GetType = swDocASSEMBLY Then  ' Units will come back as meters
  Corners = Part.GetBox(0)
Else
  MsgBox "This macro is only useful with a part or assy." & Chr$(13) & Chr$(10) & "Open one of those and try again."
  Exit Sub
End If
UserUnits = Part.GetUnits()
Select Case Part.GetUnits(0)
  Case swMM
    ConvFactor = 1 * 1000
  Case swCM
    ConvFactor = 1 * 100
  Case swMETER
    ConvFactor = 1
  Case swINCHES
    ConvFactor = 1 / 0.0254
  Case swFEET
    ConvFactor = 1 / (0.0254 * 12)
  Case swFEETINCHES
    ConvFactor = 1 / 0.0254  ' Pass inches through
  Case swANGSTROM
    ConvFactor = 10000000000#
  Case swNANOMETER
    ConvFactor = 1000000000
  Case swMICRON
    ConvFactor = 1000000
  Case swMIL
    ConvFactor = (1 / 0.0254) * 1000
  Case swUIN
    ConvFactor = (1 / 0.0254) * 1000000
End Select
Height = Round((Abs(Corners(4) - Corners(1)) * ConvFactor), UserUnits(3)) ' Z axis
Width = Round((Abs(Corners(5) - Corners(2)) * ConvFactor), UserUnits(3))  ' Y axis
Length = Round((Abs(Corners(3) - Corners(0)) * ConvFactor), UserUnits(3)) ' X axis
' Check for either (Feet-Inches OR Inches) AND fractions.  If so, return Ft-In
If (UserUnits(0) = 5 Or UserUnits(0) = 3) And UserUnits(1) = 2 Then
  Height = DecimalToFeetInches(Height, Val(UserUnits(2)))
  Width = DecimalToFeetInches(Width, Val(UserUnits(2)))
  Length = DecimalToFeetInches(Length, Val(UserUnits(2)))
End If
Call SortDimensions

End Sub




Function DecimalToFeetInches(DecimalLength As Variant, Denominator As Integer) As String
  ' converts decimal inches to feet/inches/fractions
  Dim intFeet As Integer
  Dim intInches As Integer
  Dim intFractions As Integer
  Dim FractToDecimal As Double
  Dim remainder As Double
  Dim tmpVal As Double

  ' compute whole feet
  intFeet = Int(DecimalLength / 12)
  remainder = DecimalLength - (intFeet * 12)
  tmpVal = CDbl(Denominator)

  intInches = Int(remainder)
  remainder = remainder - intInches

  If Not (remainder = 0) Then
    If Not (Denominator = 0) Then
      FractToDecimal = 1 / tmpVal
        If FractToDecimal > 0 Then
          intFractions = Int(remainder / FractToDecimal)
          If (remainder / FractToDecimal) - intFractions > 0 Then
            intFractions = intFractions + 1
          End If
        End If
     End If
  End If
  Call FractUp(intFeet, intInches, intFractions, Denominator) ' Simplify up & down

  DecimalToFeetInches = LTrim$(Str$(intFeet)) & "'-"
  DecimalToFeetInches = DecimalToFeetInches & LTrim$(Str$(intInches))
  If intFractions > 0 Then
    DecimalToFeetInches = DecimalToFeetInches & " "
    DecimalToFeetInches = DecimalToFeetInches & LTrim$(Str$(intFractions))
    DecimalToFeetInches = DecimalToFeetInches & "/" & LTrim$(Str$(Denominator))
  End If

  DecimalToFeetInches = DecimalToFeetInches & Chr$(34)

End Function
Function FractUp(InputFt As Integer, InputInch As Integer, InputNum As Integer, InputDenom As Integer)

  While InputNum Mod 2 = 0 And InputDenom Mod 2 = 0
    InputNum = InputNum / 2
    InputDenom = InputDenom / 2
  Wend

  If InputDenom = 1 Then  ' Full inch
    InputInch = InputInch + 1
    InputNum = 0
    If InputInch = 12 Then  ' Full foot
      InputFt = InputFt + 1
      InputInch = 0
    End If
  End If
End Function
'---------------------------------------------------------------------------------
Sub SortDimensions()
    Dim arr(1 To 3) As Double
    arr(1) = Length
    arr(2) = Width
    arr(3) = Height
    SortArr arr
End Sub

Sub SortArr(arr() As Double)
    Dim i As Long
    Dim j As Long
    Dim tmp As String
    Dim p As String

    For i = LBound(arr) To UBound(arr) - 1
        For j = i + 1 To UBound(arr)
            If arr(i) > arr(j) Then
                tmp = arr(i)
                arr(i) = arr(j)
                arr(j) = tmp
            End If
        Next j
    Next i
    '---------------------------------------------------------------------------------
   MsgBox (arr(1) & " x " & arr(2) & " x " & arr(3))
   StockSize = (arr(1) & " x " & arr(2) & " x " & arr(3))


Set swCustProp = Part.Extension.CustomPropertyManager("")
retval = Part.DeleteCustomInfo2("", "外形尺寸") 'Remove existing properties

swCustProp.Add3 "外形尺寸", swCustomInfoText, StockSize, 1 'Add latest values
'   ---------------------------------------------------------------------------------
End Sub

SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

27

主题

1348

帖子

3136

金币

传奇

Rank: 8Rank: 8

积分
10382

最佳新人活跃会员热心会员宣传达人灌水之王突出贡献

16#
发表于 2021-8-5 10:18:23 | 只看该作者

   经典案例图书
'定义solidwork

'这个宏是获取最大外形尺寸的:
Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Dim Height As Variant
Dim Width As Variant
Dim Length As Variant
Dim Corners As Variant
Dim retval As Boolean
Dim UserUnits As Variant
Const swDocPart = 1
Const swDocASSEMBLY = 2
'Enum swLengthUnit_e
Const swMM = 0
Const swCM = 1
Const swMETER = 2
Const swINCHES = 3
Const swFEET = 4
Const swFEETINCHES = 5
Const swANGSTROM = 6
Const swNANOMETER = 7
Const swMICRON = 8
Const swMIL = 9
Const swUIN = 10
'Enum swFractionDisplay_e
Const swNONE = 0
Const swDECIMAL = 1
Const swFRACTION = 2

Dim swCustPropMgr As SldWorks.CustomPropertyManager
Dim swModel As SldWorks.ModelDoc2
Dim StockSize As String

'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

Dim a As Integer

Dim b As String

Dim m As String

Dim e As String

Dim k As String

Dim t As String

Dim c As String

Dim j As Integer

Dim strmat As String

Dim tempvalue As String



Sub Main()

'link solidworks

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Set SelMgr = Part.SelectionManager

swApp.ActiveDoc.ActiveView.FrameState = 1

'设定变量

c = swApp.ActiveDoc.GetTitle() '零件名

strmat = Chr(34) + Trim("SW-Material" + "@") + c + Chr(34)

blnretval = Part.DeleteCustomInfo2("", "代号")

blnretval = Part.DeleteCustomInfo2("", "名称")

blnretval = Part.DeleteCustomInfo2("", "材料")

a = InStr(c, " ") - 1      '重点:分隔标识符,这里是一个空格

If a > 0 Then

    k = Left(c, a)

    t = Left(LTrim(e), 3)

    If t = "GBT" Then

        e = "GB/T" + Mid(k, 4)

    Else

        e = k

    End If

    b = Mid(c, a + 2)

    t = Right(c, 7)

    If t = ".SLDPRT" Or t = ".SLDASM" Then

        j = Len(b) - 7

    Else

        j = Len(b)

    End If

    m = Left(b, j)

End If

blnretval = Part.AddCustomInfo3("", "代号", swCustomInfoText, e)  '代号

blnretval = Part.AddCustomInfo3("", "名称", swCustomInfoText, m)  '名称

blnretval = Part.AddCustomInfo3("", "表面处理", swCustomInfoText, " ")

'End Sub'停此句
'Sub Main()'停此句

Dim arr(1 To 3) As Double

Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc

If Part Is Nothing Then     ' Did we get anything?
  MsgBox "You need to have a part or assy open at this point." & Chr$(13) & Chr$(10) _
          & Chr$(10) & "Open one and try again."
  Exit Sub
End If
If (Part.GetType = swDocPart) Then
  Corners = Part.GetPartBox(True)         ' True comes back as system units - meters
ElseIf Part.GetType = swDocASSEMBLY Then  ' Units will come back as meters
  Corners = Part.GetBox(0)
Else
  MsgBox "This macro is only useful with a part or assy." & Chr$(13) & Chr$(10) & "Open one of those and try again."
  Exit Sub
End If
UserUnits = Part.GetUnits()
Select Case Part.GetUnits(0)
  Case swMM
    ConvFactor = 1 * 1000
  Case swCM
    ConvFactor = 1 * 100
  Case swMETER
    ConvFactor = 1
  Case swINCHES
    ConvFactor = 1 / 0.0254
  Case swFEET
    ConvFactor = 1 / (0.0254 * 12)
  Case swFEETINCHES
    ConvFactor = 1 / 0.0254  ' Pass inches through
  Case swANGSTROM
    ConvFactor = 10000000000#
  Case swNANOMETER
    ConvFactor = 1000000000
  Case swMICRON
    ConvFactor = 1000000
  Case swMIL
    ConvFactor = (1 / 0.0254) * 1000
  Case swUIN
    ConvFactor = (1 / 0.0254) * 1000000
End Select
Height = Round((Abs(Corners(4) - Corners(1)) * ConvFactor), UserUnits(3)) ' Z axis
Width = Round((Abs(Corners(5) - Corners(2)) * ConvFactor), UserUnits(3))  ' Y axis
Length = Round((Abs(Corners(3) - Corners(0)) * ConvFactor), UserUnits(3)) ' X axis
' Check for either (Feet-Inches OR Inches) AND fractions.  If so, return Ft-In
If (UserUnits(0) = 5 Or UserUnits(0) = 3) And UserUnits(1) = 2 Then
  Height = DecimalToFeetInches(Height, Val(UserUnits(2)))
  Width = DecimalToFeetInches(Width, Val(UserUnits(2)))
  Length = DecimalToFeetInches(Length, Val(UserUnits(2)))
End If
Call SortDimensions

End Sub




Function DecimalToFeetInches(DecimalLength As Variant, Denominator As Integer) As String
  ' converts decimal inches to feet/inches/fractions
  Dim intFeet As Integer
  Dim intInches As Integer
  Dim intFractions As Integer
  Dim FractToDecimal As Double
  Dim remainder As Double
  Dim tmpVal As Double

  ' compute whole feet
  intFeet = Int(DecimalLength / 12)
  remainder = DecimalLength - (intFeet * 12)
  tmpVal = CDbl(Denominator)

  intInches = Int(remainder)
  remainder = remainder - intInches

  If Not (remainder = 0) Then
    If Not (Denominator = 0) Then
      FractToDecimal = 1 / tmpVal
        If FractToDecimal > 0 Then
          intFractions = Int(remainder / FractToDecimal)
          If (remainder / FractToDecimal) - intFractions > 0 Then
            intFractions = intFractions + 1
          End If
        End If
     End If
  End If
  Call FractUp(intFeet, intInches, intFractions, Denominator) ' Simplify up & down

  DecimalToFeetInches = LTrim$(Str$(intFeet)) & "'-"
  DecimalToFeetInches = DecimalToFeetInches & LTrim$(Str$(intInches))
  If intFractions > 0 Then
    DecimalToFeetInches = DecimalToFeetInches & " "
    DecimalToFeetInches = DecimalToFeetInches & LTrim$(Str$(intFractions))
    DecimalToFeetInches = DecimalToFeetInches & "/" & LTrim$(Str$(Denominator))
  End If

  DecimalToFeetInches = DecimalToFeetInches & Chr$(34)

End Function
Function FractUp(InputFt As Integer, InputInch As Integer, InputNum As Integer, InputDenom As Integer)

  While InputNum Mod 2 = 0 And InputDenom Mod 2 = 0
    InputNum = InputNum / 2
    InputDenom = InputDenom / 2
  Wend

  If InputDenom = 1 Then  ' Full inch
    InputInch = InputInch + 1
    InputNum = 0
    If InputInch = 12 Then  ' Full foot
      InputFt = InputFt + 1
      InputInch = 0
    End If
  End If
End Function
'---------------------------------------------------------------------------------
Sub SortDimensions()
    Dim arr(1 To 3) As Double
    arr(1) = Length
    arr(2) = Width
    arr(3) = Height
    SortArr arr
End Sub

Sub SortArr(arr() As Double)
    Dim i As Long
    Dim j As Long
    Dim tmp As String
    Dim p As String

    For i = LBound(arr) To UBound(arr) - 1
        For j = i + 1 To UBound(arr)
            If arr(i) > arr(j) Then
                tmp = arr(i)
                arr(i) = arr(j)
                arr(j) = tmp
            End If
        Next j
    Next i
    '---------------------------------------------------------------------------------
   MsgBox (arr(1) & " x " & arr(2) & " x " & arr(3))
   StockSize = (arr(1) & " x " & arr(2) & " x " & arr(3))


Set swCustProp = Part.Extension.CustomPropertyManager("")
retval = Part.DeleteCustomInfo2("", "外形尺寸") 'Remove existing properties

swCustProp.Add3 "外形尺寸", swCustomInfoText, StockSize, 1 'Add latest values
'   ---------------------------------------------------------------------------------
End Sub

SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

0

主题

10

帖子

12

金币

天使

Rank: 2Rank: 2

积分
50
QQ
17#
发表于 2021-8-5 13:07:00 | 只看该作者
加入QQ群
参与讨论和学习

SolidWorks技术交流群

或扫描二维码加入

SolidWorks机械工程师网,顶一下。
SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

5

主题

104

帖子

315

金币

堂主

Rank: 4

积分
921

最佳新人活跃会员热心会员宣传达人

18#
发表于 2021-8-11 10:51:43 | 只看该作者

   经典案例图书
试着摸索一下,把前面声明部分统一合并逐次运行
SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

5

主题

428

帖子

53

金币

长老

Rank: 6Rank: 6Rank: 6

积分
2273

最佳新人活跃会员热心会员宣传达人灌水之王突出贡献

19#
发表于 2021-8-11 22:50:07 | 只看该作者
感谢楼主分享,很不错!
SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

5

主题

576

帖子

182

金币

长老

Rank: 6Rank: 6Rank: 6

积分
2577

最佳新人活跃会员热心会员宣传达人灌水之王突出贡献

20#
发表于 2021-8-12 13:07:38 | 只看该作者
楼主辛苦了!
SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

5

主题

428

帖子

53

金币

长老

Rank: 6Rank: 6Rank: 6

积分
2273

最佳新人活跃会员热心会员宣传达人灌水之王突出贡献

21#
发表于 2021-8-15 13:58:28 | 只看该作者
SW机械工程师网,找到组织了!
SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

5

主题

428

帖子

53

金币

长老

Rank: 6Rank: 6Rank: 6

积分
2273

最佳新人活跃会员热心会员宣传达人灌水之王突出贡献

22#
发表于 2021-8-21 19:02:12 | 只看该作者
感谢楼主分享,很不错!
SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

8

主题

58

帖子

24

金币

侠客

Rank: 3Rank: 3Rank: 3

积分
400

最佳新人活跃会员热心会员宣传达人

23#
 楼主| 发表于 2021-8-24 10:38:38 | 只看该作者
design100 发表于 2021-8-5 10:18
'定义solidwork

'这个宏是获取最大外形尺寸的:

辛苦了!十分感谢
SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

8

主题

58

帖子

24

金币

侠客

Rank: 3Rank: 3Rank: 3

积分
400

最佳新人活跃会员热心会员宣传达人

24#
 楼主| 发表于 2021-8-24 10:41:52 | 只看该作者
Allate 发表于 2021-8-5 09:31
提供一个简单的办法——逐个点名运行(即在原来两个宏的同一文件夹下,新增加一个点名宏)。
具体需要你自 ...

辛苦了!十分感谢
SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭 卷起
关闭 卷起

手机版|小黑屋| GMT+8, 2024-4-25 19:10 , Processed in 0.426757 second(s), 38 queries .

SolidWorks机械工程师网 ( 鲁ICP备14025122号-2 ) 鲁公网安备 37028502190335号

声明:本网言论纯属发表者个人意见,与本网立场无关。
如涉版权,可发邮件: admin@swbbsc.com

快速回复 返回顶部 返回列表