Debugging Tips for SolidWorks API Programmers - VBA.pdf



Comments



Description

Debugging Tips for SolidWorks API ProgrammersVersion 1.1 Written by: Keith Rice, Technical Director CADSharp LLC www.cadsharp.com Questions to ask yourself if you’re stuck while debugging:  Are you sure you have arguments correct? o For example, consider the following line. Note that no “@” was appended after “Point1@Origin”, consequently the entity name was wrong and nothing was selected. swModel.Extension.SelectByID2 "Point1@Origin" & strCompName & "@" & strAssyName, _ "EXTSKETCHPOINT", 0, 0, 0, True, 1, Nothing, 0 Are you sure the API call does what you think it does? Are you sure you are using the correct API call? o For example, sometimes people get ISldWorks::ActivateDoc and ISldWorks::ActiveDoc confused. Are you sure that you aren’t trying to force the method do something it cannot do? o Example: if you rename a drawing sheet to “Sheet1” but a sheet with that name already exists o Example: if you have two planar faces selected and try to apply a concentric mate    Error prevention tips:  Type enumerations and member names in lowercase, so that when your cursor moves the next line, you can see if certain letters “snapped” to uppercase, indicating that the name is recognized.  Use Intellisense’s auto-complete  If you use Find and Replace; be sure to thoroughly test your code afterward either as an argument or as a return value" Page 12 – "Compile error: Method or data member not found" Page 13 – "Run-time error '424': Object required" Page 14 – "Compile error: Expected user-defined type. not project" Page 15 – "Run-time error '-2147417851 (80010105)': Automation error The server threw an exception. Page 3 – “Compile error: variable not defined” Page 4 – "Run-time error '450': Wrong number of arguments or invalid property assignment" Page 5 – "Run-time error '91': Object variable or With block variable not set" Page 6 – "Run-time error '5': Invalid procedure call or argument" Page 7 – "Run-time error '13': Type mismatch" Page 8 – "Code execution has been interrupted" Page 9 – "Run-time error '438': Object doesn't support this property or method" Page 10 – "Run-time error '-2147417848 (80010108)': Automation error The object invoked has disconnected from its clients" Page 11 – "Run-time error '98': A property or method call cannot include a reference to a private object." Page 16 – "Run-time error '445': Object doesn't support this action" Page 17 – "Compile error: Can't find project or library" Page 18 – “Compile error: User-defined type not defined” .Common VBA Errors The follow pages are an assortment of compile or run-time errors that may occur while programming SolidWorks using Visual Basic for Applications. Cause: You may have Option Explicit present and did not declare a variable . GetTwoAdjacentFaces(2) The two should not be in parentheses: swFaces = CircularEdgeCollection.GetTwoAdjacentFaces2 .Item(i).Item(i).Cause: You probably mistyped something Example: swFaces = CircularEdgeCollection. in which case calls to IAttribute members will cause this error. . You may need to create an attribute definition with a different name or restart SolidWorks.ActiveDoc Set swCustPropMgr = swModel.Print swCustPropMgr. the 3rd element does not exist For i = 0 To 1 Set swFace(i) = swSelMgr. swFace(i+1) should only be swFace(i). or the line that was supposed to Set that variable failed to do so. Example 1: If no part is open and you try to run this you will get the error on Set swCustPropMgr Dim swApp As SldWorks.Count End Sub Example 2: In this next example.SldWorks Dim swModel As SldWorks. Example 4: This error can also occur when programming attributes.GetSelectedObject6(i + 1.Cause: You didn’t Set the variable used on that particular line in particular.SldWorks Set swModel = swApp. IAttributeDef::CreateInstance5 can fail.ModelDoc2 Dim swCustPropMgr As SldWorks.CustomPropertyManager Sub main() Set swApp = Application.GetSurface Next i Example 3: This error can also occur if a method requires an array of doubles and you declare the array as a variant. If the attribute definition already exists and fails to register.CustomPropertyManager("") Debug. -1) Set swSurface(i) = swFace(i + 1).Extension. Count vFaces = collCircEdges. .Item(i) Next i Cause: There is nothing in the collection.Example: Dim vFaces As Variant Dim i As Integer For i = 0 To collCircEdges. because the code earlier that added to the collection was commented out. GetNames For Each vName In vCustPropNames Debug.GetSurface Example 4: Dim swApp as SldWorks.GetEdges Example 2: Or.GetEdges Should be vEdges = swFace. such as a variable not getting set properly.SldWorks Dim swModel as SldWorks.ModelDoc2 . if vCustPropNames is Empty: vCustPropNames = swCustPropMgr. or a method being run on an empty variable.Print " " & vName Next vName Example 3: Set swSurface1 = vFaces(0) Should be Set swSurface1 = vFaces(0). Example 1: Set vEdges = swFace.SldWork s and not SldWorks.ActiveDoc End Sub You will get a type mismatch error on Set swModel … because you declared swModel as SldWorks.Cause: This can occur for many different reasons.SldWorks Sub main() Set swApp = Application. or when you accidentally declare an object as the wrong type.SldWorks Set swModel = swApp. .GetFirstLoop While Not swLoop Is Nothing 'If Not swLoop.GetNext Wend The line in red should be "Set swLoop = swLoop.GetNext".Add swEdges(i) ' End If ' Next i 'End If swLoop.IsCircle Then ' 'add to a circular face collection ' collCircEdges.Cause: If you are trying to traverse entities and you misused or left out the code that gets the next entity.IsOuter Then ' swEdges = swLoop.GetCurve ' If swCurve.GetEdges ' For i = 0 To UBound(swEdges) ' Set swCurve = swEdges(i). Example: Set swLoop = swFace. Cause: This can occur if the object was not set.GetNext -->Needs "Set" in front Wend Example 2: If swSelMgr. This can also occur if you try using an API call that doesn’t exist in the currently referenced SolidWorks type libraries. If you tried using this API call in a macro or add-in that is referencing earlier type libraries.swSelEDGES Then vEdges(i) = swSelMgr. Example 1: Set swLoop = swFace.GetSelectedObjectType3(i.GetSelectedObject6(i.GetFirstLoop While Not swLoop Is Nothing ' Do something here swLoop = swLoop. . -1) = swSelectType_e. -1) End If Need to include Set in front of vEdges(i) Example 3: IAssemblyDoc::InsertNewVirtualPart became available in the SolidWorks 2010 type library. you’ll get this error. Then as soon as a member from the IModelDoc2 interface is called. the error will occur: Dim swApp As SldWorks. Example 2: Also. you lost access to that object’s interface because the object reference no longer exists. (In other words. Certain API calls will destroy object pointer. the IModelDoc2 pointer is going to be lost as soon as the document is closed. you may have accidentally destroyed the object pointer yourself in your code. . In the following example.CloseDoc swModel.) Example 1: Please see the macro examples involving safe entities on how to use safe entities to prevent an entity object pointer from being destroyed.ModelDoc2 Sub main() Set swApp = Application.Cause: The pointer to an object was lost. when an IBody2 member is used on an IBody2 pointer that doesn’t exist anymore. You will need to re-obtain the IBody2 pointer.SldWorks Dim swModel As SldWorks.ForceRebuild3 False End Sub Example 3: This can also occur in macro features in the rebuild routine. even though you may not have wanted that to occur.GetTitle swModel.SldWorks Set swModel = swApp.ActiveDoc swApp. The solution is to change the instancing from Private to PublicNotCreatable in the class module’s properties. Frequently this will happen when a macro written on one computer is run on a different computer. .Cause: This occurs when a module’s instancing is on Private and not PublicNotCreatable. Cause: You probably misspelled an API call. . not an object.Cause: You may have tried to set an object equal to an API call that returns an array. Try unchecking control-related references and see if the problem disappears.SldWorks Set swDoc = swApp. “Null” is not used in VBA to indicate that an object is nothing. you see that IPartDoc::GetBodies2 returns an array. True) End Sub The line in red is incorrect.swSolidBody.SldWorks Dim swDoc As SldWorks.Body2 Sub main() Set swApp = Application.swBodyType_e. Null The last argument should be Nothing. After the declaration for swBodies is renamed to “vBodies” and declared as type Variant. . You may have also used the argument “Null” instead of Nothing.swBodyType_e. True) Example 2: swFilletFeatData. For example.GetBodies2(SwConst. Example 3: This can also occur if you have unrecognized references checked relating to controls. you may get this with the Visible property of a control.swSolidBody. First. if you look in the API Help. Example 1: Dim swApp As SldWorks.ModelDoc2 Dim swPart As SldWorks. the second-to-last line should be corrected to this: vBodies = swPart.AccessSelections swModel. you do not use Set with an array. not an object.PartDoc Dim swBodies As SldWorks.GetBodies2(SwConst. Second.ActiveDoc Set swPart = swDoc Set swBodies = swPart. rather than… Dim swApp As SldWorks.Cause: This can occur if you write… Dim swApp as SldWorks …during your variable declaration. .SldWorks. Cause: This can occur when an object needs to be passed as an argument. This can also occur if you try to run a method on a lightweight component or feature that requires the component or feature to be fully resolved. Dim swApp As SldWorks.ModelDoc2 Dim swSelMgr As SldWorks.SelectionMgr Dim swLoop As SldWorks. . beginning with IFeature::GetFirstDisplayDimension.SelectionManager Set swFace = swSelMgr.GetSelectedObject6(1.GetLoops For i = 0 To UBound(vLoops) Set swLoop = vLoops(i) vEdges = swLoop. -1) vLoops = swFace.SldWorks Set swModel = swApp.Edge Dim vLoops As Variant Dim vEdges As Variant Dim i As Integer Dim j As Integer Sub main() Set swApp = Application.Loop2 Dim swEdge As SldWorks.Select Nothing.GetEdges For j = 0 To UBound(vEdges) Set swEdge = vEdges(j) swLoop. This might also occur if IDisplayDimension::GetNext5 is used instead of IFeature::GetNextDisplayDimension when trying to traverse all dimensions in a feature.ActiveDoc Set swSelMgr = swModel. Nothing Next j Next i End Sub The Nothing in red needs to be changed to “swEdge” (without quotes). Example: Note: you need to select a face before running this code. True. and Nothing is passed instead.SldWorks Dim swModel As SldWorks. ” . According to the API Help.Cause: This occurs when you use an API call that is not available. Use ISldWorks::CloseDoc. “This function is currently unavailable. Example: If you use the API call “IModelDoc2::Close”. To fix this. remove and re-add the references in ToolsReferences within the VB Editor.Cause: This can happen when a macro is run on a new computer. or copy all of the code into a new macro and run that macro. The SolidWorks Type libraries may not be loaded in correctly. . Cause: This can occur if you try to implement a class that doesn’t exist in the current type library. you would get this compile error. If you had the SolidWorks 2011 type library referenced and tried to implement this class. . Example: The class PropertyManagerPage2Handler9 became available in SolidWorks 2012.
Copyright © 2024 DOKUMEN.SITE Inc.