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

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1639|回复: 8
打印 上一主题 下一主题

在新参考基准面添加穿透约束时为什么有时报错?VBA.NET

  [复制链接]

6

主题

93

帖子

723

金币

长老

Rank: 6Rank: 6Rank: 6

积分
1409

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

跳转到指定楼层
楼主
 楼主| 发表于 2020-9-6 20:32:07 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

   经典图书
程序创建一个单螺杆

螺杆叶面是矩形,用中心矩形的中心对其螺旋线的一个端点,做穿透约束。

打开solid works 然后在vs中运行代码可以通过,但是再次运行时就报错。
注销电脑,从新进入又正常。
不打开solid works单独运行代码报错,打开sldworks,运行代码有时报错,关闭sldworks,重启sldworks本身报错,

关闭sldworks,重启sldworks本身报错


错误详情

未处理System.Runtime.InteropServices.COMException
  ErrorCode=-2147417851
  HResult=-2147417851
  Message=服务器出现意外情况。 (异常来自 HRESULT:0x80010105 (RPC_E_SERVERFAULT))
  Source=swCreate single-screw
  StackTrace:
       在 SldWorks.IModelDoc2.SketchAddConstraints(String IdStr)
       在 swCreate_single_screw.Form1.Button1_Click(Object sender, EventArgs e) 位置 D:\NXopenCode\swCreate single-screw\swCreate single-screw\Form1.vb:行号 62
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       在 swCreate_single_screw.My.MyApplication.Main(String[] Args) 位置 17d14f5c-a337-4978-8281-53493378c1071.vb:行号 81
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException:




代码如下:

  1. Imports SldWorks
  2. Imports SwConst
  3. Imports System.Runtime.InteropServices
  4. Imports System

  5. Public Class Form1

  6.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

  7.         Dim swApp As SldWorks.SldWorks
  8.         Dim swDoc As ModelDoc2 = Nothing
  9.         Dim swPart As PartDoc = Nothing
  10.         Dim swDrawing As DrawingDoc = Nothing
  11.         Dim swAssembly As AssemblyDoc = Nothing
  12.         Dim boolstatus As Boolean = False
  13.         Dim longstatus As Integer = 0
  14.         Dim longwarnings As Integer = 0
  15.         '
  16.         '建立于solid works的连接
  17.         swApp = CreateObject("SldWorks.application")
  18.         Dim swSheetWidth As Double
  19.         swSheetWidth = 0
  20.         Dim swSheetHeight As Double
  21.         swSheetHeight = 0
  22.         swDoc = CType(swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2017\templates\gb_part.prtdot", 0, swSheetWidth, swSheetHeight), ModelDoc2)
  23.         swPart = swDoc
  24.         swApp.ActivateDoc2("Part3", False, longstatus)
  25.         swDoc = CType(swApp.ActiveDoc, ModelDoc2)
  26.         Dim myModelView As ModelView = Nothing
  27.         myModelView = CType(swDoc.ActiveView, ModelView)
  28.         myModelView.FrameState = CType(swWindowState_e.swWindowMaximized, Integer)
  29.         boolstatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
  30.         swDoc.SketchManager.InsertSketch(True)
  31.         swDoc.ClearSelection2(True)
  32.         Dim skSegment As SketchSegment = Nothing
  33.         '前视基准面 画圆 r=0.05m
  34.         skSegment = CType(swDoc.SketchManager.CreateCircleByRadius(0, 0, 0, 0.05), SketchSegment)
  35.         swDoc.ClearSelection2(True)
  36.         swDoc.SketchManager.InsertSketch(True)
  37.         '前视基准面 插入螺旋线 半径0.05m 导程0.05m 起始角度0
  38.         swDoc.InsertHelix(False, True, False, True, 0, 0.05, 0.05, 1, 0, 0)
  39.         boolstatus = swDoc.Extension.SelectByRay(0.05, 0, 0, 0, 0.05, 1, 0.001, 1, True, 0, 0)
  40.         boolstatus = swDoc.Extension.SelectByID2("Unknown", "POINTREF", 0.049999999999999961, 0, 0.049999999999999975, True, 1, Nothing, 0)
  41.         Dim myRefPlane As RefPlane = Nothing
  42.         myRefPlane = CType(swDoc.FeatureManager.InsertRefPlane(2, 0, 4, 0, 0, 0), RefPlane)
  43.         swDoc.ClearSelection2(True)
  44.         '在螺旋线的端点上插入参考平面,参考平面垂直螺旋线
  45.         boolstatus = swDoc.Extension.SelectByID2("Plane1", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
  46.         swDoc.SketchManager.InsertSketch(True)
  47.         swDoc.ClearSelection2(True)
  48.         boolstatus = swDoc.Extension.SetUserPreferenceToggle(CType(swUserPreferenceToggle_e.swSketchAddConstToRectEntity, Integer), CType(swUserPreferenceOption_e.swDetailingNoOptionSpecified, Integer), True)
  49.         boolstatus = swDoc.Extension.SetUserPreferenceToggle(CType(swUserPreferenceToggle_e.swSketchAddConstLineDiagonalType, Integer), CType(swUserPreferenceOption_e.swDetailingNoOptionSpecified, Integer), True)
  50.         Dim vSkLines As Array = Nothing
  51.         vSkLines = CType(swDoc.SketchManager.CreateCenterRectangle(0, 0.008, 0, 0.028, 0.0115, 0), Array)
  52.         '画中心点正方形0.056*0.007m
  53.         swDoc.ClearSelection2(True)
  54.         '清除选择然后选择正方形中心点和螺旋线,添加穿透约束
  55.         boolstatus = swDoc.Extension.SelectByID2("Point1", "SKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)
  56.         boolstatus = swDoc.Extension.SelectByID2("Helix/Spiral1", "REFERENCECURVES", 0, 0, 1, True, 0, Nothing, 0)
  57.         'boolstatus = swDoc.Extension.SelectByRay(0.05, 0, 0, 0, 0.05, 1, 0.001, 1, True, 0, 0) 选不中螺旋线
  58.         'MsgBox("选择螺旋线准备穿透约束")
  59.         swDoc.SketchAddConstraints("sgATPIERCE") '添加穿透约束
  60.         swDoc.ClearSelection2(True)
  61.         swDoc.SketchManager.InsertSketch(True)

  62.         boolstatus = swDoc.Extension.SelectByID2("Sketch2", "SKETCH", 0, 0, 0, False, 1, Nothing, 0)
  63.         boolstatus = swDoc.Extension.SelectByID2("Helix/Spiral1", "REFERENCECURVES", 0, 0, 0, True, 4, Nothing, 0)
  64.         Dim myFeature As Feature = Nothing
  65.         myFeature = CType(swDoc.FeatureManager.InsertProtrusionSwept4(False, False, 0, False, False, 1, 1, False, 0, 0, 0, 10, True, True, True, 0, True, False, 0.01, False), Feature)
  66.         swDoc.ClearSelection2(True)
  67.         '
  68.         'Save As
  69.         longstatus = swDoc.SaveAs3("D:\UG\Part3.SLDPRT", 0, 2)
  70.         '
  71.         'Close Document
  72.         swPart = Nothing
  73.         swDoc = Nothing
  74.         swApp.CloseDoc("Part3.SLDPRT")
  75.     End Sub
  76. End Class
复制代码


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

使用道具 举报

6

主题

93

帖子

723

金币

长老

Rank: 6Rank: 6Rank: 6

积分
1409

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

沙发
 楼主| 发表于 2020-9-6 20:39:52 | 只看该作者
'boolstatus = swDoc.Extension.SelectByRay(0.05, 0, 0, 0, 0.05, 1, 0.001, 1, True, 0, 0) 在新建的参考基准面"Plane1"中选不中螺旋线,但同样的语句在前视基准面"Front Plane"中可以选中螺旋线。

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

使用道具 举报

6

主题

93

帖子

723

金币

长老

Rank: 6Rank: 6Rank: 6

积分
1409

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

板凳
 楼主| 发表于 2020-9-6 20:44:11 | 只看该作者

   经典图书
源文件
swCreate single-screw.rar (445.02 KB, 下载次数: 51)
SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

6

主题

93

帖子

723

金币

长老

Rank: 6Rank: 6Rank: 6

积分
1409

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

地板
 楼主| 发表于 2020-9-7 18:46:32 | 只看该作者
为在单位点电脑,设置solidwoks为中文菜单,家里设置solidwoks为英文菜单
在单位调试时发现有的参数必须改回和菜单对应点设置语言,才能执行,

报错可能对象没选择上。
SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

1

主题

66

帖子

181

金币

侠客

Rank: 3Rank: 3Rank: 3

积分
354
QQ
5#
发表于 2020-9-8 17:15:32 | 只看该作者

   经典案例图书
很不错,顶一下!
SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

6

主题

93

帖子

723

金币

长老

Rank: 6Rank: 6Rank: 6

积分
1409

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

6#
 楼主| 发表于 2020-9-8 22:23:54 | 只看该作者
添加穿透约束,螺旋线的端点选不中,谁有办法?
SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

6

主题

93

帖子

723

金币

长老

Rank: 6Rank: 6Rank: 6

积分
1409

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

7#
 楼主| 发表于 2020-9-8 23:20:51 | 只看该作者

   经典案例图书
这个程序是成功的,solid works设置成英文界面
  1. Imports SldWorks
  2. Imports SwConst
  3. Imports System.Runtime.InteropServices
  4. Imports System

  5. Public Class Form1

  6.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

  7.         Dim swApp As SldWorks.SldWorks
  8.         Dim swDoc As ModelDoc2 = Nothing
  9.         Dim swPart As PartDoc = Nothing
  10.         Dim swDrawing As DrawingDoc = Nothing
  11.         Dim swAssembly As AssemblyDoc = Nothing
  12.         Dim boolstatus As Boolean = False
  13.         Dim longstatus As Integer = 0
  14.         Dim longwarnings As Integer = 0
  15.         '
  16.         '建立于solid works的连接
  17.         swApp = CreateObject("SldWorks.Application")
  18.         Dim swSheetWidth As Double
  19.         swSheetWidth = 0
  20.         Dim swSheetHeight As Double
  21.         swSheetHeight = 0
  22.         swDoc = CType(swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2017\templates\gb_part.prtdot", 0, swSheetWidth, swSheetHeight), ModelDoc2)
  23.         swPart = swDoc
  24.         swApp.ActivateDoc2("Part1", False, longstatus)
  25.         swDoc = CType(swApp.ActiveDoc, ModelDoc2)
  26.         Dim myModelView As ModelView = Nothing
  27.         myModelView = CType(swDoc.ActiveView, ModelView)
  28.         myModelView.FrameState = CType(swWindowState_e.swWindowMaximized, Integer)
  29.         boolstatus = swDoc.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
  30.         swDoc.SketchManager.InsertSketch(True)
  31.         swDoc.ClearSelection2(True)
  32.         Dim skSegment As SketchSegment = Nothing
  33.         '前视基准面 画圆 r=0.05m
  34.         skSegment = CType(swDoc.SketchManager.CreateCircleByRadius(0, 0, 0, 0.05), SketchSegment)
  35.         swDoc.ClearSelection2(True)
  36.         swDoc.SketchManager.InsertSketch(True)
  37.         '前视基准面 插入螺旋线 半径0.05m 导程0.05m 起始角度0
  38.         swDoc.InsertHelix(False, True, False, True, 0, 0.05, 0.05, 1, 0, 0)
  39.         '在螺旋线的端点0.05, 0, 0上插入参考平面,参考平面垂直螺旋线
  40.         boolstatus = swDoc.Extension.SelectByRay(0, 0, 0, 1, 0, 0, 0.0008, 1, True, 0, 0) 'TypeWanted=swSelEDGES 1 "EDGE"
  41.         boolstatus = swDoc.Extension.SelectByID2("Unknown", "POINTREF", 0.05, 0, 0, True, 1, Nothing, 0)
  42.         Dim myRefPlane As RefPlane = Nothing
  43.         myRefPlane = CType(swDoc.FeatureManager.InsertRefPlane(2, 0, 4, 0, 0, 0), RefPlane) '
  44.         swDoc.ClearSelection2(True)
  45.         '选择新建的参考平面"Plane1" 画中心点正方形
  46.         boolstatus = swDoc.Extension.SelectByID2("Plane1", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
  47.         swDoc.SketchManager.InsertSketch(True)
  48.         'swDoc.ClearSelection2(True)
  49.         '画中心点正方形前设置正方形中间的结构线开关为on
  50.         boolstatus = swDoc.Extension.SetUserPreferenceToggle(CType(swUserPreferenceToggle_e.swSketchAddConstToRectEntity, Integer), CType(swUserPreferenceOption_e.swDetailingNoOptionSpecified, Integer), True)
  51.         boolstatus = swDoc.Extension.SetUserPreferenceToggle(CType(swUserPreferenceToggle_e.swSketchAddConstLineDiagonalType, Integer), CType(swUserPreferenceOption_e.swDetailingNoOptionSpecified, Integer), True)
  52.         Dim vSkLines As Array = Nothing
  53.         vSkLines = CType(swDoc.SketchManager.CreateCenterRectangle(0, 0.005, 0, 0.028, 0.0085, 0), Array) '画中心点正方形0.056*0.007m
  54.         'MsgBox("", MsgBoxStyle.OkOnly)
  55.         swDoc.ClearSelection2(True)
  56.         boolstatus = swDoc.Extension.SelectByID2("Point1", "SKETCHPOINT", 0, 0.005, 0, True, 0, Nothing, 0)
  57.         'MsgBox("选中矩形中心点", MsgBoxStyle.OkOnly)
  58.         boolstatus = swDoc.Extension.SelectByID2("Helix/Spiral1", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0) '选择螺旋线
  59.         'boolstatus = swDoc.Extension.SelectByRay(0.0401858737063126, -0.12968008300873635, 0.02107012441977929, 0, 0.98757049215139314, -0.15717672547758266, 0.00051512576414589788, 1, True, 0, 0)
  60.         swDoc.SketchAddConstraints("sgATPIERCE")
  61.         swDoc.ClearSelection2(True)
  62.         swDoc.SketchManager.InsertSketch(True)

  63.         boolstatus = swDoc.Extension.SelectByID2("Sketch2", "SKETCH", 0, 0, 0, False, 1, Nothing, 0)
  64.         boolstatus = swDoc.Extension.SelectByID2("Helix/Spiral1", "REFERENCECURVES", 0, 0, 0, True, 4, Nothing, 0)
  65.         Dim myFeature As Feature = Nothing
  66.         myFeature = CType(swDoc.FeatureManager.InsertProtrusionSwept4(False, False, 0, False, False, 1, 1, False, 0, 0, 0, 10, True, True, True, 0, True, False, 0.01, False), Feature)
  67.         swDoc.ClearSelection2(True)
  68.         '
  69.         'Save As
  70.         longstatus = swDoc.SaveAs3("D:\UG\Part3.SLDPRT", 0, 2)
  71.         '
  72.         'Close Document
  73.         swPart = Nothing
  74.         swDoc = Nothing
  75.         swApp.CloseDoc("Part3.SLDPRT")
  76.     End Sub
  77. End Class
复制代码


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

使用道具 举报

6

主题

93

帖子

723

金币

长老

Rank: 6Rank: 6Rank: 6

积分
1409

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

8#
 楼主| 发表于 2020-9-8 23:24:05 | 只看该作者
SelectByRay在参考基准面 怎么选中螺旋线,求帮助?
SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

6

主题

93

帖子

723

金币

长老

Rank: 6Rank: 6Rank: 6

积分
1409

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

9#
 楼主| 发表于 2020-9-10 09:08:27 | 只看该作者
可能因为选不中螺旋线报错
SolidWorks机械工程师网
回复 支持 反对

使用道具 举报

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

本版积分规则

SOLIDWORKS 2023 机械设计从入门到精通

手机版|小黑屋| GMT+8, 2025-5-25 22:27 , Processed in 0.167175 second(s), 23 queries , Memcache On.

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

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

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