找回密码 注册 QQ登录
开思网工业级高精度在线3D打印服务

iCAx开思网

CAD/CAM/CAE/设计/模具 高清视频【积分说明】如何快速获得积分?快速3D打印 手板模型CNC加工服务在线3D打印服务,上传模型,自动报价
查看: 64843|回复: 126
打印 上一主题 下一主题

[原创] VB.NET FOR UG 二次开发 教程(附全部源码)--.NET 开发的里程碑

[复制链接]
跳转到指定楼层
1
发表于 2009-9-15 16:13:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多同行朋友,交流,分享,学习。

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

x
先做一个小广告,希望斑竹不要生气哦。。哈。。下面是我可以提供的服务,如果苏州有公司需求的话请联系我 Q:191269819,手机:13812950814

1 UG标准化定制: 根据公司标准需求,定制UG的默认设置,统一公司标准
2 UG标准件库开发: 根据公司设计和开发的需求,开发和维护标准件库
3 UG CAD 二次开发:根据公司的设计流程,开发能提高设计效率的程序
4 UG CAM 二次开发:根据公司的加工标注和流程,定制和开发适合于CAM的模版和程序
5 QC CMM检测系统:根据公司的设计,加工和检测流程,在UG中开发出测量工具,方便CMM系统自动检测数据
6 工厂使用工具的开发::如给NCEDM使用的一些工具


教程的主要内容为:
1.怎么创建对象(5个例子)
2.怎么选择对象(5个例子)
3.怎么搜索对象(5个例子)
。。以后再补充。。

本帖最后由 苏州人 于 2009-9-22 16:28 编辑
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 赞一下!赞一下!
2
发表于 2009-9-15 16:13:56 | 只看该作者
第一个例子:怎样用VB.NET在UG中创建一个点?
Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

Module CreatePoint

    Dim s As Session = Session.GetSession()
    Dim ufs As UFSession = UFSession.GetUFSession()

    Sub Main()

        Dim sp As New Point3d(0, 0, 0)

        Dim thePoint As Point = s.Parts.Work.Points.CreatePoint(sp)

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY

    End Function

End Module

有问题欢迎提问。。。。

本帖最后由 苏州人 于 2009-9-16 16:52 编辑
3
发表于 2009-9-16 16:51:41 | 只看该作者
第二个例子:怎样用VB.NET在UG中创建一个条线?

Option Strict Off  

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

Module template_code

  Dim s As Session = Session.GetSession()

Sub Main()

  Dim sp As New Point3d(0, 0, 0)
  Dim ep As New Point3d(10, 10, 0)

  Dim theLine As Line = s.Parts.Work.Curves.CreateLine(sp, ep)

        
End Sub

  Public Function GetUnloadOption(ByVal dummy As String) As Integer

      GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY

  End Function

End Module
4
发表于 2009-9-17 13:08:10 | 只看该作者
不错,希望老大出一点,
5
发表于 2009-9-18 07:37:45 | 只看该作者
老大,你发的太少了,不舍得传授啊
6
发表于 2009-9-18 10:10:28 | 只看该作者
第三个例子:怎样用VB.NET在UG中创建一个圆柱,然后更改它的颜色?

Option Strict Off  
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

Module create_a_cylinder_and_set_color

    Sub Main()

        Dim s As Session = Session.GetSession()
        Dim ufs As UFSession = UFSession.GetUFSession()

        Dim wp As Part = s.Parts.Work()

        Dim cyl_feat_tag As NXOpen.Tag
        Dim orig() As Double = {1, 1, 0}
        Dim dir() As Double = {1, 1, 1}

        ufs.Modl.CreateCylinder(FeatureSigns.Nullsign, Nothing, orig, "50", _
            "25", dir, cyl_feat_tag)

        Dim cyl_body_tag As NXOpen.Tag

        ufs.Modl.AskFeatBody(cyl_feat_tag, cyl_body_tag)
        Dim cyl_body As Body = CType(NXObjectManager.Get(cyl_body_tag), Body)

        MsgBox("Color change", MsgBoxStyle.Information, "Current Operation:")
        cyl_body.Color = 3
        cyl_body.RedisplayObject()

        s.Preferences.ScreenVisualization.FitPercentage = 95
        wp.Views.WorkView.Fit()


  end Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY

    End Function

End Module

本帖最后由 苏州人 于 2009-9-18 10:11 编辑
7
发表于 2009-9-18 17:50:30 | 只看该作者
支持一下,我提个问题。比如一个大圆内,有两个互不相连的小圆,同时选中这三个圆,然后生成一个平的面,结果是一个大圆面上有两个圆孔。

  用NX的命令很容易实现,二次开发如何实现呢?能否给个函数?
8
发表于 2009-9-19 13:45:07 | 只看该作者
你说的我不是特别理解,下面按我理解的解释吧
我感觉你的结果是要做一个圆柱,然后上面有两个孔吧?

其实二次开发是按照手动建模的步骤去设计的,针对你这个问题
(1)选择三个圆,记下这三个圆的TAG
(2)用ufs.Modl.createextruded() 函数分别去实现这三个圆的拉伸,结果为三个圆柱Cy1,Cy2,Cy3
(3)用ufs.Modl.subtractBodies()在Cy1去减去Cy2,Cy3就可以了。

不过需要注意的是用ufs.Modl.subtractBodies()中的两个参数是body tag,而不是feat tag,需要把feat tag 转化成body tag 用这个函数 ufs.Modl.AskFeatBody( ,  )

用ufs.modl.create...()函数做出来的tag都是feat tag.....
9
发表于 2009-9-19 13:48:21 | 只看该作者
第四个例子:怎样用VB.NET在UG中创建注释?

Option Strict Off  

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

Module create_note

  Dim s As Session = Session.GetSession()
  Dim ufs As UFSession = UFSession.GetUFSession()

Sub Main()

    Dim theNote As NXOpen.Tag
Try
    Dim workPart As Part = s.Parts.Work
    Dim workPartTag As NXOpen.Tag = workPart.Tag

Catch ex As Exception
    ufs.Ui.OpenListingWindow()
    ufs.Ui.WriteListingWindow(ex.GetBaseException.ToString())
    ufs.Ui.WriteListingWindow(vbCrLf & "+++ Work Part Required" & vbCrLf)
    Return
End Try


    Dim num_lines As Integer = 2
    Dim textString As String() = {"This is the first line.", _
                                  "This is the second line."}
    Dim origin_3d() As Double = {6, 6, 0}
    Dim orientation As Integer = 0 ' zero is Horizontal, 1 is Vertical

Try

    ufs.Drf.CreateNote(num_lines, textString, origin_3d, _
                    orientation, theNote)

Catch ex As Exception
    ufs.Ui.OpenListingWindow()
    ufs.Ui.WriteListingWindow(ex.GetBaseException.ToString())
    ufs.Ui.WriteListingWindow(vbCrLf & "+++ Note not created" & vbCrLf)
End Try

End Sub

  Public Function GetUnloadOption(ByVal dummy As String) As Integer

      GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY

  End Function

End Module
10
发表于 2009-9-21 15:06:06 | 只看该作者
第五个例子:怎样用VB.NET在UG中创建两个体然后做布尔加操作?

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

Module template_code

Sub Main()

  Dim s As Session = Session.GetSession()
  Dim ufs As UFSession = UFSession.GetUFSession()

  '
  ' ------------------------------------------------- make sure we have a part
  Dim this_part As NXOpen.Tag
Try
  this_part = s.Parts.Work.Tag
Catch ex As Exception

  If this_part = NXOpen.Tag.Null Then
    MsgBox("You need an open part to run this program.", MsgBoxStyle.OKOnly)
    ' no part, so exit program gracefully
    Exit Sub
  End If

End Try

  '
  ' ------------------------------------------------- first solid: a block

  Dim corner_pt(2) As Double
  Dim block_feat_tag As NXOpen.Tag
  Dim edge_lengths(2) As String

  ' This is an alternate way to set the string value:
  '
  'Dim lengths(2) As Double
  'lengths(0) = 150.1234
  'edge_lengths(0) = lengths(0).ToString
  '
  ' but setting it this way, we get the expression to boot:

  edge_lengths(0) = "xlen=150.1234"
  edge_lengths(1) = "ylen=65.4321"
  edge_lengths(2) = "thickness=25."

  Dim sign As FeatureSigns

  sign = FeatureSigns.Nullsign

  corner_pt(0) = 20
  corner_pt(1) = 30
  corner_pt(2) = -10

  ufs.Modl.CreateBlock1(sign, corner_pt, edge_lengths, block_feat_tag)

  If block_feat_tag <> NXOpen.Tag.Null Then
    ufs.View.FitView(NXOpen.Tag.Null, 1.0)
    MsgBox("First Solid Body tag is: " & block_feat_tag.ToString)
  End If

  '
  ' ------------------------------------------------- second solid: a cylinder

  Dim height As String
  Dim diameter As String
  Dim direction(2) As Double
  Dim cyl_feat_tag As NXOpen.Tag

  height = "cyl_height=90"
  diameter = "cyl_dia=40"

  direction(0) = 0.707
  direction(1) = 0.707
  direction(2) = 0.707

  ufs.Modl.CreateCyl1(sign, corner_pt, height, diameter, direction, cyl_feat_tag)

  If cyl_feat_tag <> NXOpen.Tag.Null Then
    ufs.View.FitView(NXOpen.Tag.Null, 1.0)
    MsgBox("Second Solid Body tag is: " & cyl_feat_tag.ToString)
  End If

  '
  ' ------------------------------------------------- unite the two solids

  Dim block_body_tag As NXOpen.Tag
  Dim cyl_body_tag As NXOpen.Tag

  ufs.Modl.AskFeatBody(block_feat_tag, block_body_tag)
  ufs.Modl.AskFeatBody(cyl_feat_tag, cyl_body_tag)

  ufs.Modl.UniteBodies(block_body_tag, cyl_body_tag)

  '
  ' ------------------------------------------------- report count of solids

  Dim all_bodies() As Body = s.Parts.Work.Bodies.ToArray()
  Dim body_count As Integer

  body_count = all_bodies.Length

  MsgBox("Count of Bodies now in Work Part: " & body_count)

End Sub

  Public Function GetUnloadOption(ByVal dummy As String) As Integer

      GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY

  End Function

End Module

>>>关于创建方面的开发到此为止

本帖最后由 苏州人 于 2009-9-21 15:07 编辑
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3D打印手板模型快速制作服务,在线报价下单!

QQ 咨询|手机版|联系我们|iCAx开思网  

GMT+8, 2025-1-31 07:41 , Processed in 0.032882 second(s), 10 queries , Gzip On, Redis On.

Powered by Discuz! X3.3

© 2002-2025 www.iCAx.org

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