Hi,
I want to replace Part model in all the views of a drawing file,
I am using the below code:
ModelDoc2 Drawingfile = (ModelDoc2)swApp_Active.GetOpenDocumentByName(NEW_FILE_PATH.Replace(".SLDPRT", ".SLDDRW"));
if (Drawingfile == null)
Drawingfile = swApp_Active.OpenDoc6(NEW_FILE_PATH.Replace(".SLDPRT", ".SLDDRW"), (int)swDocumentTypes_e.swDocDRAWING, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref fileerror, ref filewarning);
DrawingDoc swDrawing = (DrawingDoc)Drawingfile;
bool success = false;
try
{
ModelDocExtension swModelDocExt;
SelectionMgr swSelectionMgr;
DrawingComponent swDrawingComponent;
View swView;
Component2 swComponent;
object view;
object instance;
object[] views = new object[swDrawing.GetViewCount()];
object[] instances = new object[swDrawing.GetViewCount()];
DispatchWrapper[] viewsIn = new DispatchWrapper[swDrawing.GetViewCount()];
DispatchWrapper[] instancesIn = new DispatchWrapper[swDrawing.GetViewCount()];
bool status;
object[] All_Views = (object[])swDrawing.GetViews();
IView[] Vviews = (IView[])All_Views;
int v = 0;
foreach (IView vv in Vviews)
{
//View Curr_view = swDrawing.IActiveDrawingView();// GetView(i);
status = swDrawing.ActivateView(vv.Name);
//Select the view in which to replace the model
swModelDocExt = (ModelDocExtension)Drawingfile.Extension;
status = swModelDocExt.SelectByID2(vv.Name, "DRAWINGVIEW", 0, 0, 0, false, 0, null, 0);
swSelectionMgr = (SelectionMgr)Drawingfile.SelectionManager;
swView = (View)swSelectionMgr.GetSelectedObject6(1, -1);
view = (object)swView;
views[v] = view;
viewsIn[v] = new DispatchWrapper(views[v]);
// Select the instance of the model to replace
status = swModelDocExt.SelectByID2(vv.GetReferencedModelName(), "COMPONENT", 0, 0, 0, false, 0, null, 0);
swDrawingComponent = (DrawingComponent)swSelectionMgr.GetSelectedObject6(1, -1);
swComponent = (Component2)swDrawingComponent.Component;
instance = (object)swComponent;
instances[v] = instance;
instancesIn[v] = new DispatchWrapper(instances[v]);
v++;
}
success = swDrawing.ReplaceViewModel(NEW_FILE_PATH, (viewsIn), (instancesIn));
}
catch (Exception ex)
{
MessageBox.Show("Method 2 Error :" + ex.HResult);
//throw;
}I am getting the below error:
Message = "Unable to cast object of type 'System.Object[]' to type 'SolidWorks.Interop.sldworks.IView[]'."
could anyone guide me how the rectify the above issue.
