Here is what I am trying to do. I am creating a setup sheet for the machine shop with all the listing of tool holders, tools, and offsets. I have written a macro that does the following operation:
1- opens an Excel spreadsheet (contains all the holder, tool & offset values)
2- Search and open the holder for station 1
3- Search and open the tool for station 1
4- Creates a new assembly
5- Inserts the holder and tool in the assembly
6- Creates a concentric and a distance mate in the assembly. Save the assembly
7- Insert the assembly into the drawing
8- Repeat steps 2 to 7 for all the stations in the spreadsheet.
Everything is working but a lot of time the mate distance goes into error mode after more assemblies are inserted into the drawing.
As you can see above, assemblies 2 and 3 have errors in it. To fix that error, I open the assembly, re-define the distance mate. Just by opening up the distance mate, the error goes away. I don't change anything. Close the mate, save and close the assembly file and the error goes away. This is the code for the distance mate creation.
'Create the distance mate for tool 1 and holder 1
swFrame.SetStatusBarText "Create Distance Mate"
If tool1Found = True Then
1001 MateName2 = "Holder-Tool Distance"
Dim distance As Double
Dim BorH As String
If DrawingUnit = "INCH" Then 'Working in imperial
If valueInBOffset > 0 Then
distance = valueInBOffset * 0.0254
BorH = "B"
Else
distance = valueInHOffset * 0.0254
BorH = "H"
End If
Else 'Working in metric
If valueInBOffset > 0 Then
distance = valueInBOffset * 0.001
BorH = "B"
Else
distance = valueInHOffset * 0.001
BorH = "H"
End If
End If
SelectComponentFaceByName (holder1FileNameTrunkated & "-1"), "HolderDistance1" 'Look for a face name HolderDistance1 in the holder file
If BorH = "H" Then 'Search for the H or B Plane, Face, or Edge
FindDistanceSelection (toolName & "-1"), "ToolDistanceH"
Else
FindDistanceSelection (toolName & "-1"), "ToolDistanceB"
End If
Set matefeature2 = assembly.AddMate5(swMateDISTANCE, 2, True, distance, 0#, 0.001, 0.001, 0.001, 0#, 0#, 0#, False, False, 0, longstatus) 'Create the Distance Mate feature
If matefeature2 Is Nothing Then 'Check to see if the mate was created
response = MsgBox("Distance Mate was not created, Do you want to edit the tool and try to create the mate again?", vbYesNo)
If response = vbYes Then
Set Part = swApp.ActivateDoc3(Tool1FileName, False, swRebuildOnActivation_e.swUserDecision, longwarnings)
Set sMgr = Part.SelectionManager
Part.ClearSelection2 True
Call wait
swApp.ActivateDoc2 Tool1FileName, False, longstatus
swApp.CloseDoc Tool1FileName
GoTo 1001
End If
Else
matefeature2.Name = MateName2
assembly.EditRebuild3
End If
assembly.ClearSelection2 True
End If
My thinking is that once I go to station 2, creating the new concentric and distance mate for that station affects the first. Any thoughts?
Denis
SolidworksApi/macros