iCAx开思网

标题: mwd121,有空的话帮我看看下面的代码。 [打印本页]

作者: hun    时间: 2004-5-3 00:12
标题: mwd121,有空的话帮我看看下面的代码。
我的代码要实现的功能事要实现把一个文件存到SmarTeam的库里面去,麻烦高手帮我调一下。我是用VB做的
'function create engine, session, open database connect
Public Sub cmdLogin()
  Dim SmDatabase As SmDatabase
  Dim SmGuiStore As ISmGUIStore
   
    On Error GoTo ErrExit
  
    Screen.MousePointer = vbHourglass
    
    'create new engine
    Set Engine = New SmApplic.SmEngine
    'init engine
    Engine.Init "SmTeam32"
  
    'create session object
    Set Session = Engine.CreateSession("Api_Training", "SmTeam32.ini")
    'Set Session = New SmApplic.SmSession
  
    Set SmDatabase = Engine.Databases(0)
    'connect to database
    Session.OpenDatabaseConnection SmDatabase.Alias, SmDatabase.Password, True
   
    'Session.OpenDatabaseConnection "SmartDB", "szuwir", True
    
    Set GUIService = Session.GetService("SmGUISrv.SmGUIServices")
    
    Set SmGuiStore = GUIService.GUIStore
    If (GUIService.GUIStore.LogIn = False) Then
      MsgBox "connect failed"
      Exit Sub
    End If
    'user login with session
    'Session.UserLogin "joe", ""
    
    Screen.MousePointer = vbNormal
    'MsgBox "Connection Successful!"
    Set SmCommonGUI = Session.GetService("SmGUISrv.SmCommonGUI")
    'SmGuiStore.OpenQueryByAttribute (swsNormal)
    Exit Sub
ErrExit:
    MsgBox Err.Description
End Sub
Public Sub Add_File3()          'Save as file
  Dim NewObject As SmApplic.ISmObject 'new sm object
Dim ClassId As Integer
Dim SmClass As ISmClass
Dim LinkObject As ISmObject
Dim RootObject As ISmObject
Dim MainObject As ISmObject
Dim LinkClassid As Integer
Dim Behavior As ISmBehavior
Dim SmAttributes As ISmClassAttributes
Dim SmAttribute As ISmClassAttribute
Dim Value As String
Dim Sequence As ISmSequence
Dim PrmIdAttrs As SmRecordList
Dim PrmIdRec As SmRecord
Dim FolderObj As ISmObject
Dim LinkAttribute As SmRecord
Dim Folder_ClassId As Integer
  
Dim SaveasDialog As ISmSaveAsDialog
Dim SmClass_1 As ISmClass
Dim SmClass_2 As ISmClass
Dim ChildClasses As ISmClasses
Dim MainClassObject As ISmObject
Dim Selected_Parent As ISmObject
Dim Parent_ClassId As Integer
Dim Parent_ObjectId As Integer
Dim Parent_Object As ISmObject
'下面这一句调不通,还有其他的问题。
    Set SmClass = Session.MetaInfo.SmClassByName("SolidWorks Assembly")
    ClassId = SmClass.ClassId
    
    ' Create new SmObject
    Set NewObject = Session.ObjectStore.NewObject(ClassId)
    ' Adds all object attributes to its data
    NewObject.AddAllAttributes
    NewObject.SetDefaultValues
    Set SmAttributes = NewObject.SmClass.Attributes
    Set SmAttribute = SmAttributes.ItemByIndex(2) 'CN_ID
    
    Set Sequence = Session.ObjectStore.Sequences.ItemByAttribute(SmAttribute)
    Value = Sequence.GetCurrentValue
    Sequence.IncrementValue Value, True
    Value = Sequence.GetCurrentValue
    
    ' Here you must assign values at least to mandatory , obligatory
    ' and primary attributes, otherwise an error message will appear
    ' on the Insert operation
    ' ...
    NewObject.Data.Value("CN_ID") = Value
    ' Save object to the database
    NewObject.Data.Value("CN_DESCRIPTION") = "SolidWorks Assembly_3"
    NewObject.Data.Value("FILE_NAME") = "Mainclinchassm.sldasm"
    NewObject.Data.Value("DIRECTORY") = "e:\api_training\solidworks"
    
    LinkClassid = 0
    NewObject.Insert
    
    
    Set SmClass_1 = Session.MetaInfo.SmClassByName("SolidWorks Assembly")
    Set SmClass_2 = Session.MetaInfo.SmClassByName("Documents")
    Set ChildClasses = SmClass_2.AllChildClasses
    'set MainClassObject as Project trees
    Set MainClassObject = Session.ObjectStore.NewObject(1)
    MainClassObject.ObjectId = -1
  
    Set SaveasDialog = SmCommonGUI.Dialogs.NewSaveAsDialog
    
    SaveasDialog.FileName = "Mainclinchassm.sldasm"
    SaveasDialog.SelectedClass = SmClass_1
    SaveasDialog.Classes = ChildClasses
    SaveasDialog.SelectedMainClassObject = MainClassObject
    
    SaveasDialog.ShowModal
  
    If (SaveasDialog.ModalResult <> mrOK) Then
      Exit Sub
    End If
    
    Set Selected_Parent = SaveasDialog.SelectedParent
   &nbsparent_ObjectId = Selected_Parent.ObjectId
   &nbsparent_ClassId = Selected_Parent.ClassId
    
    If (Parent_ClassId = SmClass_2.ClassId) Then
       Set MainClassObject = SaveasDialog.SelectedMainClassObject
      &nbsparent_ObjectId = MainClassObject.ObjectId
       MsgBox Parent_ObjectId
      &nbsparent_ClassId = MainClassObject.ClassId
       If ((Parent_ObjectId = -1) And (Parent_ClassId = 1)) Then
         MsgBox "You should Select Projects!"
         Exit Sub
       End If
       Set Parent_Object = Session.ObjectStore.RetrieveObject(Parent_ClassId, Parent_ObjectId)
       NewObject.AddToDesktop Parent_Object, LinkClassid, LinkObject, RootObject
      
    Else
    
      Folder_ClassId = Session.MetaInfo.SmClassByName("Folder").ClassId
      'ObjectId = Selected_Parent.ObjectId
      If (Selected_Parent.ClassId <> Folder_ClassId) Then
        MsgBox "You can't create the link "
        Exit Sub
      End If
      LinkClassid = Session.MetaInfo.HierarchicalLinkClassByClasses(Selected_Parent.ClassId, NewObject.ClassId)
      Set LinkObject = NewObject.LinkToParent(LinkClassid, Selected_Parent, LinkAttribute)
      
    End If
  
End Sub
'下面代码我在窗体上建了一个按钮来调用的
Private Sub Command1_Click()
Call cmdLogin
Add_File3
End Sub
'感谢高手能帮帮忙,上面的的代码有不少问题,主要是有一些变量不懂的定义。
作者: Duron.900    时间: 2004-5-3 09:58
看不懂,好难:(
作者: hun    时间: 2004-5-3 15:54
我还加我以下全局变量,
Option Explicit
Dim Session As SmApplic.SmSession
Dim GUIService As SmGUISrv.SmGUIServices
  
Dim Engine As SmEngine
Dim SmCommonGUI As SmCommonGUI
Dim GuiStore As ISmGUIStore
这样以后 Set SmClass = Session.MetaInfo.SmClassByName("SolidWorks Assembly")  
能通过编译了,但我在操作的时候SmarTeam告诉我无法建立连接。
我的代码要实现的功能事要实现把一个文本文件存到SmarTeam的库里面去,麻烦高手帮我调一下。我是用VB做的
作者: mwd121    时间: 2004-5-5 17:17
我将你的程序试了一下,没有问题!你再看一下,注意类名是否正确!
你的程序需要增加生命周期操作才能将文件真正地检入电子仓库,否则只是建立了一个数据库对象罢了!
作者: hun    时间: 2004-5-6 01:33
谢谢,我再分析试试
作者: jy00517793    时间: 2006-12-25 10:22
直接用存放电子仓库的方法试试




欢迎光临 iCAx开思网 (https://www.icax.org/) Powered by Discuz! X3.3