找回密码
 注册
查看: 2519|回复: 0

[教程] 高版本Sw编制的API代码,90%代码能在低版本SW的API中应用。

[复制链接]
发表于 2017-5-3 13:17:08 | 显示全部楼层 |阅读模式

马上注册,查询更多机械资源,享用更多功能,轻松畅享机械设计招标网。

您需要 登录 才可以下载或查看,没有账号?注册

×
用Sw2014编制的零件、装配休和工程图,绝对不可能在低版本的SW中应用。
而在高版本solidworks中编制的API文件,90%代码能在低版本的SolidWorks中得到应用。


************************************
看了n个版本的Api帮助文件,90%以上的语句,适合SolidWorks所有版本。
只是高版本有些语句在低版本不可用。如CustomProperties和measure语句,只能在Sw2007-Sw2012版本应用。

绝大多数Api代码,不但能在Sw2007-Sw2012应用,还且还能在Sw2006应用。只不过遇到CustomProperties和measure语句时,会提示出错,需要编写几条代码实现CustomProperties和measure语句的功能。

例如:SolidWorks任何版本的网上帮助文件,在介绍CustomProperties时,对Sw2006和Sw2007进行了比较。

2010 Solidworks api Help - Obsolete and New Custom Properties Methods and Properties
http://help.SolidWorks.com/2010/English/api/sldworksapiprogguide/miscellaneous/obsolete_and_new_custom_properties_methods_and_properties.htm
*****************************************************************************************


SolidWorks 2006以下版本用CustomInfo代码
  1. Function CustInfoName(SwModel As ModelDoc2)
  2.   Dim SwConfig As Configuration, ConfArr, ConfName, CustArr
  3.   Dim Str, kk
  4.     kk = 2
  5.     ConfArr = SwModel.GetConfigurationNames
  6.     For ii = 0 To UBound(ConfArr)
  7.         Set SwConfig = SwModel.GetConfigurationByName(ConfArr(ii))
  8.         ConfName = SwConfig.Name
  9.         With SwModel
  10.            Str = Chr(34) & "SW-Mass@@" & ConfName & "@" & .GetTitle & Chr(34)
  11.            .AddCustomInfo3 ConfName, "质量", 30, "12"
  12.            .CustomInfo2(ConfName, "质量") = Str '"""" & Str & """"
  13.            Str = Chr(34) & "SW-Material@@" & ConfName & "@" & .GetTitle & Chr(34)         
  14.            CustArr = .GetCustomInfoNames2(ConfName)
  15.            .ShowConfiguration ConfName
  16.         End With
  17.     Next ii
  18. End Function
复制代码



*************************************


SolidWorks 2007以下版本用CustomPropertyManager


代码
  1. Sub ll()
  2.   Dim SwModel As ModelDoc2
  3.   Set SwModel = Application.SldWorks.ActiveDoc
  4.   Dim swCustProp As CustomPropertyManager
  5.   Set swCustProp = SwModel.Extension.CustomPropertyManager("A1")
  6.   swCustProp.Add "Mass", swCustomInfoType_e.swCustomInfoText, "MyValue"
  7.   swCustProp.Set "Mass", "MyValue"
  8. End Sub
复制代码

*************************************
UseNamedConfiguration在SolidWorks 2006以下版本没有,在SolidWorks2008以上版本能够使用。
swComp.UseNamedConfiguration = True 'Use named
swComp.UseNamedConfiguration = False

******************************************************************
Sw2006没有测量(Measure)语句, 要实现Measure语句功能,需要多几个代码。

Sw2008以后版本有Measure功能。省去几行代码,提高了编程的可读性。

  1. Function MeasureLen(SwFeat As Feature, SwMeasure As Measure)
  2.   Dim SwSketch As Sketch, SkSegArr, SkSeg As SketchSegment
  3.     Set SwSketch = SwFeat.GetSpecificFeature
  4.     SkSegArr = SwSketch.GetSketchSegments
  5.     For ii = 0 To UBound(SkSegArr)
  6.       Set SkSeg = SkSegArr(ii)
  7.       Debug.Print SkSeg.GetType, Round(SkSeg.GetLength * 1000, 1)
  8.       SkSeg.Select2 True, 1
  9.     Next
  10.     ''
  11.     With SwMeasure
  12.        boolstatus = .Calculate(Nothing)
  13.        If boolstatus Then
  14.          MeasureLen = Round(.TotalLength * 1000, 1)
  15.        Else
  16.          MsgBox "No Measure "
  17.        End If
  18.     End With
  19. End Function

  20. Private Sub ll()
  21.   Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2
  22.     Set SwApp = Application.SldWorks
  23.     Set SwModel = SwApp.ActiveDoc
  24.   Dim SwMeasure As Measure, SwFeat As Feature, Str
  25.     Set SwMeasure = SwModel.Extension.CreateMeasure
  26.     Str = "测量长度"
  27.     Set SwFeat = SwModel.FeatureByName(Str)
  28.     Debug.Print MeasureLen(SwFeat, SwMeasure)

  29. End Sub
复制代码



招标网平台地图|Archiver|手机版|机械设计招标网 ( 京ICP备17072296号-4 )

GMT+8, 2024-5-14 03:18

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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